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

cli/deposit: significantly simplify validator keystore path #1618

Merged
merged 7 commits into from
Apr 26, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ public void writeKeys(final BLSKeyPair validatorKey, final BLSKeyPair withdrawal
final KeyStoreData withdrawalKeyStoreData =
generateKeystoreData(withdrawalKey, withdrawalKeyPassword);

saveKeyStore(
keystoreDirectory.resolve("validator_" + validatorKey.getPublicKey().toString() + ".json"),
validatorKeyStoreData);
saveKeyStore(
keystoreDirectory.resolve(
"withdrawal_" + withdrawalKey.getPublicKey().toString() + ".json"),
withdrawalKeyStoreData);
final String validatorFileName =
"validator_" + trimPublicKey(validatorKey.getPublicKey().toString()) + ".json";
final String withdrawalFileName =
"withdrawal_" + trimPublicKey(withdrawalKey.getPublicKey().toString()) + ".json";

saveKeyStore(keystoreDirectory.resolve(validatorFileName), validatorKeyStoreData);
saveKeyStore(keystoreDirectory.resolve(withdrawalFileName), withdrawalKeyStoreData);
}

private Path createKeystoreDirectory(final BLSKeyPair validatorKey) {
final Path keystoreDirectory =
outputPath.resolve("validator_" + validatorKey.getPublicKey().toString());
outputPath.resolve("validator_" + trimPublicKey(validatorKey.getPublicKey().toString()));
try {
return Files.createDirectories(keystoreDirectory);
} catch (IOException e) {
Expand Down Expand Up @@ -99,4 +99,11 @@ private void saveKeyStore(final Path outputPath, final KeyStoreData keyStoreData
throw new UncheckedIOException(e);
}
}

private String trimPublicKey(final String publicKey) {
if (publicKey.toLowerCase().startsWith("0x")) {
return publicKey.substring(2, 9);
}
return publicKey.substring(0, 7);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,38 @@ void keysAreWrittenToEncryptedKeystores(@TempDir final Path tempDir) {

assertKeyStoreCreatedAndCanBeDecrypted(
tempDir.resolve(
"validator_" + validator1PubKey + "/validator_" + validator1PubKey + ".json"),
"validator_"
+ trimPublicKey(validator1PubKey)
+ "/validator_"
+ trimPublicKey(validator1PubKey)
+ ".json"),
validator1SecretKey);
assertKeyStoreCreatedAndCanBeDecrypted(
tempDir.resolve(
"validator_" + validator1PubKey + "/withdrawal_" + withdrawal1PubKey + ".json"),
"validator_"
+ trimPublicKey(validator1PubKey)
+ "/withdrawal_"
+ trimPublicKey(withdrawal1PubKey)
+ ".json"),
withdrawal1SecretKey);

keysWriter.writeKeys(new BLSKeyPair(validator2SecretKey), new BLSKeyPair(withdrawal2SecretKey));

assertKeyStoreCreatedAndCanBeDecrypted(
tempDir.resolve(
"validator_" + validator2PubKey + "/validator_" + validator2PubKey + ".json"),
"validator_"
+ trimPublicKey(validator2PubKey)
+ "/validator_"
+ trimPublicKey(validator2PubKey)
+ ".json"),
validator2SecretKey);
assertKeyStoreCreatedAndCanBeDecrypted(
tempDir.resolve(
"validator_" + validator2PubKey + "/withdrawal_" + withdrawal2PubKey + ".json"),
"validator_"
+ trimPublicKey(validator2PubKey)
+ "/withdrawal_"
+ trimPublicKey(withdrawal2PubKey)
+ ".json"),
withdrawal2SecretKey);
}

Expand All @@ -83,4 +99,11 @@ private void assertKeyStoreCreatedAndCanBeDecrypted(
assertThat(KeyStore.decrypt(PASSWORD, keyStoreData))
.isEqualTo(blsSecretKey.getSecretKey().toBytes());
}

private String trimPublicKey(final String publicKey) {
if (publicKey.toLowerCase().startsWith("0x")) {
return publicKey.substring(2, 9);
}
return publicKey.substring(0, 7);
}
}