use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.
the class KeyStoreTest method encryptWithKdfAndCipherFunction.
@ParameterizedTest
@MethodSource("encryptWithKdfAndCipherArguments")
void encryptWithKdfAndCipherFunction(final KdfParam kdfParam, final Bytes expectedChecksum, final Bytes encryptedCipherMessage) {
final KeyStoreData keyStoreData = KeyStore.encrypt(BLS_PRIVATE_KEY, BLS_PUB_KEY, PASSWORD, "", kdfParam, CIPHER);
assertThat(keyStoreData.getCrypto().getChecksum().getMessage()).isEqualTo(expectedChecksum);
assertThat(keyStoreData.getCrypto().getCipher().getMessage()).isEqualTo(encryptedCipherMessage);
assertThat(keyStoreData.getVersion()).isEqualTo(KeyStoreData.KEYSTORE_VERSION);
assertThat(keyStoreData.getPubkey()).isEqualTo(BLS_PUB_KEY);
assertThat(keyStoreData.getUuid()).isNotNull();
}
use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.
the class KeyStoreTest method encryptSaveAndReloadKeyStore.
private void encryptSaveAndReloadKeyStore(final Path tempDir, final KdfParam kdfParam) throws IOException {
final KeyStoreData keyStoreData = KeyStore.encrypt(BLS_PRIVATE_KEY, BLS_PUB_KEY, PASSWORD, "", kdfParam, CIPHER);
final Path tempKeyStoreFile = Files.createTempFile(tempDir, "keystore", ".json");
assertThatCode(() -> KeyStoreLoader.saveToFile(tempKeyStoreFile, keyStoreData)).doesNotThrowAnyException();
// reload it back
final KeyStoreData loadedKeyStore = KeyStoreLoader.loadFromFile(tempKeyStoreFile);
assertThat(loadedKeyStore.getUuid()).isEqualByComparingTo(keyStoreData.getUuid());
assertThat(loadedKeyStore.getCrypto().getChecksum().getMessage()).isEqualTo(keyStoreData.getCrypto().getChecksum().getMessage());
}
use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.
the class KeyStoreTest method invalidPasswordValidation.
private void invalidPasswordValidation(final String resourcePath) {
final KeyStoreData keyStoreData = loadKeyStoreFromResource(resourcePath);
assertThat(KeyStore.validatePassword("invalidpassword", keyStoreData)).isFalse();
}
use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.
the class KeyStore method encrypt.
/**
* Encrypt the given BLS12-381 key with specified password.
*
* @param blsPrivateKey BLS12-381 private key in Bytes to encrypt. It is not validated to be a
* valid BLS12-381 key.
* @param blsPublicKey BLS12-381 public key in Bytes. It is not validated and stored as it is.
* @param password The password to use for encryption
* @param path Path as defined in EIP-2334. Can be empty String.
* @param kdfParam crypto function such as scrypt or PBKDF2 and related parameters such as dklen,
* salt etc.
* @param cipher cipher function and iv parameter to use.
* @return The constructed KeyStore with encrypted BLS Private Key as cipher.message and other
* details as defined by the EIP-2335 standard.
*/
public static KeyStoreData encrypt(final Bytes blsPrivateKey, final Bytes blsPublicKey, final String password, final String path, final KdfParam kdfParam, final Cipher cipher) {
checkNotNull(blsPrivateKey, "PrivateKey cannot be null");
checkNotNull(blsPublicKey, "PublicKey cannot be null");
checkNotNull(password, "Password cannot be null");
checkNotNull(path, "Path cannot be null");
checkNotNull(kdfParam, "KDFParam cannot be null");
checkNotNull(cipher, "Cipher cannot be null");
kdfParam.validate();
cipher.validate();
final Crypto crypto = encryptUsingCipherFunction(blsPrivateKey, password, kdfParam, cipher);
return new KeyStoreData(crypto, blsPublicKey, path);
}
use of tech.pegasys.signers.bls.keystore.model.KeyStoreData in project signers by ConsenSys.
the class KeyStoreLoader method loadFromString.
public static KeyStoreData loadFromString(final String keystoreString) {
try {
final KeyStoreData keyStoreData = OBJECT_MAPPER.readValue(keystoreString, KeyStoreData.class);
keyStoreData.validate();
return keyStoreData;
} catch (final JsonParseException e) {
throw new KeyStoreValidationException("Invalid KeyStore: " + e.getMessage(), e);
} catch (final JsonMappingException e) {
throw convertToKeyStoreValidationException(e);
} catch (final IOException e) {
LOG.error("Unexpected IO error while reading KeyStore: " + e.getMessage());
throw new KeyStoreValidationException("Unexpected IO error while reading KeyStore: " + e.getMessage(), e);
}
}
Aggregations