Skip to content

Commit

Permalink
improve generator
Browse files Browse the repository at this point in the history
  • Loading branch information
vadiminc committed Aug 2, 2023
1 parent 9b204aa commit 12a8d42
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 71 deletions.
66 changes: 33 additions & 33 deletions dist/tsc/src/commands/actions/KeySharesCustomBulkAction.js

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

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

75 changes: 38 additions & 37 deletions src/commands/actions/KeySharesCustomBulkAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ export class KeySharesCustomBulkAction extends BaseAction {
const bulkProcess = multiShares || true;
if (bulkProcess) {
const { files } = await getKeyStoreFiles(sanitizePath(keystorePath));

// validate data
if (files.length !== operatorGroups.length) {
throw Error(`Files amoumt(${files.length}) is not equal clusters amount(${operatorGroups.length})`);
}

// validate all files
console.debug('Validating keystore files, do not terminate process!');
let validatedFilesCount = 0;
Expand All @@ -92,10 +98,9 @@ export class KeySharesCustomBulkAction extends BaseAction {
this.ownerNonce = ownerNonce;
let processedFilesCount = 0;
console.debug('Splitting keystore files to shares, do not terminate process!');
for (const file of files) {
const keySharesFiles = await this._processFile(file, password, outputFolder, operatorGroups, ownerAddress);
outputFiles = [...outputFiles, ...keySharesFiles];

for (const [index, file] of files.entries()) {
const keySharesFile = await this._processFile(file, password, outputFolder, operatorGroups[index], ownerAddress);
outputFiles.push(keySharesFile);
processedFilesCount++;
process.stdout.write(`\r${processedFilesCount}/${files.length} keystore files successfully split into shares`);
}
Expand All @@ -108,49 +113,45 @@ export class KeySharesCustomBulkAction extends BaseAction {
if (isValidPassword !== true) {
throw Error(String(isValidPassword));
}
const keySharesFiles = await this._processFile(keystorePath, password, outputFolder, operatorGroups, ownerAddress);
outputFiles = [...keySharesFiles];
const keySharesFile = await this._processFile(keystorePath, password, outputFolder, operatorGroups[0], ownerAddress);
outputFiles = [keySharesFile];
}
return outputFiles;
}

private async _processFile(keystoreFilePath: string, password: string, outputFolder: string, operatorGroups: any[], ownerAddress: string) {
private async _processFile(keystoreFilePath: string, password: string, outputFolder: string, operators: any[], ownerAddress: string) {
const keystoreData = await readFile(keystoreFilePath);

// Initialize SSVKeys SDK
const ssvKeys = new SSVKeys();
const { privateKey, publicKey } = await ssvKeys.extractKeys(keystoreData, password);

const keySharesFiles = [];
for (const [index, operators] of operatorGroups.entries()) {
// Build shares from operator IDs and public keys
const encryptedShares = await ssvKeys.buildShares(privateKey, operators);

const keyShares = new KeyShares();
await keyShares.update({
ownerAddress,
ownerNonce: this.ownerNonce,
operators,
publicKey,
});

// Build payload and save it in key shares file
await keyShares.buildPayload({
publicKey,
operators,
encryptedShares,
}, {
ownerAddress,
ownerNonce: this.ownerNonce,
privateKey,
});

const keySharesFilePath = await getFilePath('keyshares-files', outputFolder.trim(), `${index}`);
await writeFile(keySharesFilePath, keyShares.toJson());
keySharesFiles.push(keySharesFilePath);
this.ownerNonce++;
}
// Build shares from operator IDs and public keys
const encryptedShares = await ssvKeys.buildShares(privateKey, operators);

const keyShares = new KeyShares();
await keyShares.update({
ownerAddress,
ownerNonce: this.ownerNonce,
operators,
publicKey,
});

// Build payload and save it in key shares file
await keyShares.buildPayload({
publicKey,
operators,
encryptedShares,
}, {
ownerAddress,
ownerNonce: this.ownerNonce,
privateKey,
});

const keySharesFilePath = await getFilePath('keyshares-files', outputFolder.trim());
await writeFile(keySharesFilePath, keyShares.toJson());
this.ownerNonce++;

return keySharesFiles;
return keySharesFilePath;
}
}

0 comments on commit 12a8d42

Please sign in to comment.