diff --git a/artemis/src/main/java/tech/pegasys/artemis/cli/deposit/EncryptedKeystoreWriter.java b/artemis/src/main/java/tech/pegasys/artemis/cli/deposit/EncryptedKeystoreWriter.java index e6656b9fca2..afc176fb2ff 100644 --- a/artemis/src/main/java/tech/pegasys/artemis/cli/deposit/EncryptedKeystoreWriter.java +++ b/artemis/src/main/java/tech/pegasys/artemis/cli/deposit/EncryptedKeystoreWriter.java @@ -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) { @@ -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); + } } diff --git a/artemis/src/test/java/tech/pegasys/artemis/cli/deposit/EncryptedKeystoreWriterTest.java b/artemis/src/test/java/tech/pegasys/artemis/cli/deposit/EncryptedKeystoreWriterTest.java index 51295dfc62f..6cf50004f99 100644 --- a/artemis/src/test/java/tech/pegasys/artemis/cli/deposit/EncryptedKeystoreWriterTest.java +++ b/artemis/src/test/java/tech/pegasys/artemis/cli/deposit/EncryptedKeystoreWriterTest.java @@ -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); } @@ -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); + } }