Skip to content

Commit

Permalink
Rename to SecpV3KeystoresBulkLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
usmansaleem committed Sep 4, 2023
1 parent 9a974d3 commit 7ebb5c6
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import tech.pegasys.web3signer.signing.EthSecpArtifactSigner;
import tech.pegasys.web3signer.signing.SecpArtifactSignature;
import tech.pegasys.web3signer.signing.bulkloading.SecpAzureBulkLoader;
import tech.pegasys.web3signer.signing.bulkloading.SecpWalletBulkloader;
import tech.pegasys.web3signer.signing.bulkloading.SecpV3KeystoresBulkLoader;
import tech.pegasys.web3signer.signing.config.AzureKeyVaultFactory;
import tech.pegasys.web3signer.signing.config.AzureKeyVaultParameters;
import tech.pegasys.web3signer.signing.config.DefaultArtifactSignerProvider;
Expand Down Expand Up @@ -256,7 +256,7 @@ private MappedResults<ArtifactSigner> bulkloadV3Keystores() {

LOG.info("Bulk loading v3 keystore files ... ");
final MappedResults<ArtifactSigner> walletResults =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(
v3WalletBLParams.getKeystoresPath(),
v3WalletBLParams.hasKeystoresPasswordFile()
? v3WalletBLParams.getKeystoresPasswordFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,60 +31,61 @@
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;

public class SecpWalletBulkloader {
public class SecpV3KeystoresBulkLoader {
private static final Logger LOG = LogManager.getLogger();

public static MappedResults<ArtifactSigner> loadWalletsUsingPasswordFileOrDir(
final Path walletsDirectory, final Path passwordsFileOrDirectory) {
if (!Files.exists(passwordsFileOrDirectory)) {
public static MappedResults<ArtifactSigner> loadV3KeystoresUsingPasswordFileOrDir(
final Path keystoresPath, final Path pwrdFileOrDirPath) {
if (!Files.exists(pwrdFileOrDirPath)) {
LOG.error("Password file or directory doesn't exist.");
return MappedResults.errorResult();
}

final List<Path> walletFiles;
final List<Path> keystoresFiles;
try {
walletFiles = JsonFilesUtil.loadJsonExtPaths(walletsDirectory);
keystoresFiles = JsonFilesUtil.loadJsonExtPaths(keystoresPath);
} catch (final IOException e) {
LOG.error("Error listing v3 wallet paths {}", walletsDirectory);
LOG.error("Error listing v3 keystores paths {}", keystoresPath);
return MappedResults.errorResult();
}

final PasswordReader passwordReader;
if (Files.isDirectory(passwordsFileOrDirectory)) {
passwordReader =
passwordFile -> Files.readString(passwordsFileOrDirectory.resolve(passwordFile));
} else if (Files.isRegularFile(passwordsFileOrDirectory)) {
if (Files.isDirectory(pwrdFileOrDirPath)) {
passwordReader = passwordFile -> Files.readString(pwrdFileOrDirPath.resolve(passwordFile));
} else if (Files.isRegularFile(pwrdFileOrDirPath)) {
try {
final String password = Files.readString(passwordsFileOrDirectory);
final String password = Files.readString(pwrdFileOrDirPath);
passwordReader = passwordFile -> password;
} catch (final IOException e) {
LOG.error("Unable to read password file.", e);
return MappedResults.errorResult();
}
} else {
LOG.error("Unexpected password file or directory.");
LOG.error(
"Unexpected path. Expecting it to be a regular file or directory. {}", pwrdFileOrDirPath);
return MappedResults.errorResult();
}

return walletFiles.parallelStream()
.map(walletFile -> createSecpArtifactSigner(walletFile, passwordReader))
return keystoresFiles.parallelStream()
.map(keystoreFile -> createSecpArtifactSigner(keystoreFile, passwordReader))
.reduce(MappedResults.newSetInstance(), MappedResults::merge);
}

private static MappedResults<ArtifactSigner> createSecpArtifactSigner(
final Path wallet, final PasswordReader passwordReader) {
final Path v3KeystorePath, final PasswordReader passwordReader) {
try {
final String fileNameWithoutExt =
FilenameUtils.removeExtension(wallet.getFileName().toString());
FilenameUtils.removeExtension(v3KeystorePath.getFileName().toString());

final String password = passwordReader.readPassword(fileNameWithoutExt + ".txt");

final Credentials credentials = WalletUtils.loadCredentials(password, wallet.toFile());
final Credentials credentials =
WalletUtils.loadCredentials(password, v3KeystorePath.toFile());
final EthSecpArtifactSigner artifactSigner =
new EthSecpArtifactSigner(new CredentialSigner(credentials));
return MappedResults.newInstance(Set.of(artifactSigner), 0);
} catch (final IOException | CipherException | RuntimeException e) {
LOG.error("v3 Wallet could not be loaded {}", wallet, e);
LOG.error("Error loading v3 keystore {}", v3KeystorePath, e);
return MappedResults.errorResult();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,66 +27,67 @@
import org.junit.jupiter.api.io.TempDir;
import org.web3j.crypto.WalletUtils;

public class SecpWalletBulkLoaderTest {
public class SecpV3KeystoresBulkLoaderTest {

@TempDir Path walletDir;
@TempDir Path keystoreDir;
@TempDir Path passwordDir;

@BeforeEach
void initWalletAndPasswordFiles() throws Exception {
void initV3KeystoresAndPasswordFiles() throws Exception {
for (int i = 0; i < 4; i++) {
final String fileName = WalletUtils.generateLightNewWalletFile("test123", walletDir.toFile());
final String fileName =
WalletUtils.generateLightNewWalletFile("test123", keystoreDir.toFile());

final Path passwordFile =
passwordDir.resolve(fileName.substring(0, fileName.lastIndexOf(".json")) + ".txt");
Files.writeString(passwordFile, "test123");

// write files in wallet dir that will be ignored by bulk loading logic
Files.writeString(walletDir.resolve(i + ".txt"), "ignored");
Files.writeString(keystoreDir.resolve(i + ".txt"), "ignored");
}
}

@Test
void loadSecpWalletWithPasswordFilesFromDir() {
void loadSecpV3KeystoresWithPasswordFilesFromDir() {
final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(walletDir, passwordDir);
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(keystoreDir, passwordDir);

assertThat(results.getValues()).hasSize(4);
assertThat(results.getErrorCount()).isZero();
}

@Test
void loadSecpWalletWithPasswordFile() throws IOException {
void loadSecpV3KeystoresWithPasswordFile() throws IOException {
final Path passwordFile;
try (Stream<Path> passwordFiles = Files.list(passwordDir)) {
passwordFile = passwordFiles.findAny().orElseThrow();
}

final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(walletDir, passwordFile);
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(keystoreDir, passwordFile);

assertThat(results.getValues()).hasSize(4);
assertThat(results.getErrorCount()).isZero();
}

@Test
void emptyResultsWhenWalletDirIsEmpty(@TempDir Path emptyWalletDir) throws IOException {
void emptyResultsWhenKeystoresDirIsEmpty(@TempDir Path emptyDir) throws IOException {
final Path passwordFile;
try (Stream<Path> passwordFiles = Files.list(passwordDir)) {
passwordFile = passwordFiles.findAny().orElseThrow();
}

final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(emptyWalletDir, passwordFile);
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(emptyDir, passwordFile);

assertThat(results.getValues()).isEmpty();
assertThat(results.getErrorCount()).isZero();
}

@Test
void errorResultsWhenWalletFileIsInvalid() throws IOException {
void errorResultsWhenV3KeystoreFileIsInvalid() throws IOException {
for (int i = 0; i < 4; i++) {
Files.writeString(walletDir.resolve(i + ".json"), "invalid content");
Files.writeString(keystoreDir.resolve(i + ".json"), "invalid content");
}

final Path passwordFile;
Expand All @@ -95,7 +96,7 @@ void errorResultsWhenWalletFileIsInvalid() throws IOException {
}

final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(walletDir, passwordFile);
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(keystoreDir, passwordFile);

assertThat(results.getValues()).hasSize(4);
assertThat(results.getErrorCount()).isEqualTo(4);
Expand All @@ -106,7 +107,7 @@ void errorResultsWhenPasswordIsInvalid() throws IOException {
Path passwordFile = Files.writeString(passwordDir.resolve("password.txt"), "invalid");

final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(walletDir, passwordFile);
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(keystoreDir, passwordFile);

assertThat(results.getValues()).isEmpty();
assertThat(results.getErrorCount()).isEqualTo(4);
Expand All @@ -117,7 +118,7 @@ void errorResultsWhenPasswordFileIsMissing() {
Path passwordFile = passwordDir.resolve("password.txt");

final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(walletDir, passwordFile);
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(keystoreDir, passwordFile);

assertThat(results.getValues()).isEmpty();
assertThat(results.getErrorCount()).isEqualTo(1);
Expand All @@ -131,7 +132,7 @@ void errorResultsWhenCorrespondingPasswordFileIsMissing() throws IOException {
}

final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(walletDir, passwordDir);
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(keystoreDir, passwordDir);

assertThat(results.getValues()).hasSize(3);
assertThat(results.getErrorCount()).isEqualTo(1);
Expand All @@ -140,7 +141,8 @@ void errorResultsWhenCorrespondingPasswordFileIsMissing() throws IOException {
@Test
void errorResultsWhenPasswordDirIsEmpty(@TempDir Path emptyPasswordDir) {
final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(walletDir, emptyPasswordDir);
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(
keystoreDir, emptyPasswordDir);

assertThat(results.getValues()).isEmpty();
assertThat(results.getErrorCount()).isEqualTo(4);
Expand All @@ -149,8 +151,8 @@ void errorResultsWhenPasswordDirIsEmpty(@TempDir Path emptyPasswordDir) {
@Test
void errorResultsWhenPasswordDirIsMissing() {
final MappedResults<ArtifactSigner> results =
SecpWalletBulkloader.loadWalletsUsingPasswordFileOrDir(
walletDir, passwordDir.resolve("/invalid"));
SecpV3KeystoresBulkLoader.loadV3KeystoresUsingPasswordFileOrDir(
keystoreDir, passwordDir.resolve("/invalid"));

assertThat(results.getValues()).isEmpty();
assertThat(results.getErrorCount()).isEqualTo(1);
Expand Down

0 comments on commit 7ebb5c6

Please sign in to comment.