Skip to content

Commit

Permalink
Updates file saving logic and bumps to v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-miller-0 committed Mar 28, 2023
1 parent 22be985 commit 61cd0e2
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ release

# Default data output dirs
deposit-data*
bls-to-execution-change*

# Logs
logs
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lattice-cli",
"version": "0.2.0",
"version": "0.3.0",
"description": "CLI for interacting with gridplus-sdk",
"main": "./dist/lattice-cli.js",
"types": "./dist/lattice-cli.d.ts",
Expand Down
32 changes: 14 additions & 18 deletions src/commands/buildDepositData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import { AbiCoder } from '@ethersproject/abi';
import {
chmodSync,
existsSync,
mkdirSync,
writeFileSync,
} from 'fs';
import {
Client,
Constants as SDKConstants
Expand Down Expand Up @@ -32,6 +26,7 @@ import {
pathIntToStr,
pathStrToInt,
printColor,
saveFile,
startNewSpinner
} from '../utils';

Expand Down Expand Up @@ -230,20 +225,21 @@ export async function cmdGenDepositData(client: Client) {
"Where do you wish to save the deposit data files? ",
"./deposit-data"
);
if (!existsSync(fDir)) {
mkdirSync(fDir);
}
for (let i = 0; i < depositData.length; i++) {
const fPath = fDir + `/keystore-m_12381_3600_${startingIdx + i}_0_0-${datetime}.json`;
writeFileSync(fPath, keystores[i]);
// These are JSON files so they don't really need to be executable,
// but this matches the permissions from the official Ethereum Deposit CLI.
chmodSync(fPath, "710");
saveFile(
fDir,
`keystore-m_12381_3600_${startingIdx + i}_0_0-${datetime}.json`,
keystores[i],
// NOTE: These are JSON files so they don't really need to be executable,
// but this matches the permissions from the official Ethereum Deposit CLI.
"710"
);
};
const fName = exportCalldata ?
`deposit-calldata-${datetime}.json` :
`deposit-data-${datetime}.json`;
writeFileSync(fDir + '/' + fName, JSON.stringify(depositData));
saveFile(
fDir,
exportCalldata ? `deposit-calldata-${datetime}.json` : `deposit-data-${datetime}.json`,
JSON.stringify(depositData)
);
printColor(`Validator deposit data files saved to ${fDir}`, "green");
}

Expand Down
25 changes: 15 additions & 10 deletions src/commands/changeBLSCredentials.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { writeFileSync } from 'fs';
import { sha256 } from '@noble/hashes/sha256';
import {
Client,
Expand All @@ -18,6 +17,7 @@ import {
isValidEth1Addr,
pathStrToInt,
printColor,
saveFile,
startNewSpinner,
} from '../utils';

Expand Down Expand Up @@ -161,23 +161,28 @@ export async function cmdChangeBLSCredentials(client: Client) {
}

if (signedMsgs.length > 0) {
const fDir = await promptForString(
"Where do you wish to save the deposit data files? ",
"./bls-to-execution-change"
);
// Write the file
const fName = `bls_to_execution_change-${Date.now()}.json`;
writeFileSync(fName, JSON.stringify(signedMsgs));
saveFile(
fDir,
fName,
JSON.stringify(signedMsgs),
// NOTE: These are JSON files so they don't really need to be executable,
// but this matches the permissions from the official Ethereum Deposit CLI.
"710"
)
// Tell the user how to use it
printColor(
`\n\n` +
`=======================\n` +
`Generated credential change data for ${signedMsgs.length} validators ` +
`(${validatorIndices.join(', ')})\n` +
`Wrote to file: ${fName}\n` +
`=======================\n` +
`Please move this file to the machine running your consensus client ` +
`and run the following command:\n` +
`\n-----------------------\n` +
`curl -d @${fName} -H "Content-Type: application/json" -X POST 127.0.0.1:4000/eth/v1/beacon/pool/bls_to_execution_changes` +
`\n-----------------------\n\n` +
`For more details, please see this guide: https://notes.ethereum.org/@launchpad/withdrawals-guide`,
`Wrote to file: ${fDir + '/' + fName}\n` +
`=======================\n`,
'green'
);
}
Expand Down
21 changes: 21 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import crypto from "crypto";
import {
chmodSync,
existsSync,
mkdirSync,
writeFileSync,
} from 'fs';
import Spinnies from "spinnies";
let spinner: Spinnies;

Expand Down Expand Up @@ -124,4 +130,19 @@ export function getDecimalPlaces(num: number): number {
const str = num.toString();
const decimalIndex = str.indexOf('.');
return decimalIndex === -1 ? 0 : str.length - decimalIndex - 1;
}

/**
* Save a file to a directory with optional permissions.
* Create the directory if it doesn't exist.
*/
export function saveFile(fDir: string, fName: string, data: string, perm?: string) {
if (!existsSync(fDir)) {
mkdirSync(fDir);
}
const path = fDir + '/' + fName;
writeFileSync(path, data);
if (perm) {
chmodSync(path, perm);
}
}

0 comments on commit 61cd0e2

Please sign in to comment.