use of org.apache.tuweni.bytes.Bytes48 in project teku by ConsenSys.
the class ChainDataProvider method validatorParameterToIndex.
private Optional<Integer> validatorParameterToIndex(final tech.pegasys.teku.spec.datastructures.state.beaconstate.BeaconState state, final String validatorParameter) {
if (!isStoreAvailable()) {
throw new ChainDataUnavailableException();
}
if (validatorParameter.toLowerCase().startsWith("0x")) {
final Bytes48 keyBytes = getBytes48FromParameter(validatorParameter);
try {
return spec.getValidatorIndex(state, BLSPublicKey.fromBytesCompressed(keyBytes));
} catch (IllegalArgumentException ex) {
return Optional.empty();
}
}
try {
final UInt64 numericValidator = UInt64.valueOf(validatorParameter);
if (numericValidator.isGreaterThan(UInt64.valueOf(Integer.MAX_VALUE))) {
throw new BadRequestException(String.format("Validator Index is too high to use: %s", validatorParameter));
}
final int validatorIndex = numericValidator.intValue();
final int validatorCount = state.getValidators().size();
if (validatorIndex > validatorCount) {
return Optional.empty();
}
return Optional.of(validatorIndex);
} catch (NumberFormatException ex) {
throw new BadRequestException(String.format("Invalid validator: %s", validatorParameter));
}
}
use of org.apache.tuweni.bytes.Bytes48 in project teku by ConsenSys.
the class ValidatorTest method equalsReturnsFalseWhenPubkeysAreDifferent.
@Test
void equalsReturnsFalseWhenPubkeysAreDifferent() {
Bytes48 differentPublicKey = BLSTestUtil.randomPublicKey(99).toBytesCompressed();
Validator testValidator = new Validator(differentPublicKey, withdrawalCredentials, effectiveBalance, slashed, activationEligibilityEpoch, activationEpoch, exitEpoch, withdrawalEpoch);
assertNotEquals(pubkey, differentPublicKey);
assertNotEquals(validator, testValidator);
}
use of org.apache.tuweni.bytes.Bytes48 in project teku by ConsenSys.
the class BLSPublicKeyTest method fromBytesCompressedValidate_throwsOnInvalidPubKey.
@Test
void fromBytesCompressedValidate_throwsOnInvalidPubKey() {
Bytes48 invalidPublicKeyBytes = Bytes48.fromHexString("0x9378a6e3984e96d2cd50450c76ca14732f1300efa04aecdb805b22e6d6926a85ef409e8f3acf494a1481090bf32ce3bd");
assertThatThrownBy(() -> BLSPublicKey.fromBytesCompressedValidate(invalidPublicKeyBytes)).isInstanceOf(IllegalArgumentException.class);
}
use of org.apache.tuweni.bytes.Bytes48 in project teku by ConsenSys.
the class BlstPublicKeyTest method succeedsWhenInvalidPublicKeyIsInvalid.
@Test
void succeedsWhenInvalidPublicKeyIsInvalid() {
Bytes48 invalidPublicKeyBytes = Bytes48.fromHexString("0x9378a6e3984e96d2cd50450c76ca14732f1300efa04aecdb805b22e6d6926a85ef409e8f3acf494a1481090bf32ce3bd");
assertThatThrownBy(() -> {
BlstPublicKey publicKey = BlstPublicKey.fromBytes(invalidPublicKeyBytes);
publicKey.forceValidation();
}).isInstanceOf(IllegalArgumentException.class);
}
use of org.apache.tuweni.bytes.Bytes48 in project web3signer by ConsenSys.
the class KeystoreUtil method createKeystoreFile.
public static void createKeystoreFile(final BLSKeyPair keyPair, final Path keystoreDir, final String password) {
final KdfParam kdfParam = new Pbkdf2Param(32, 2, HMAC_SHA256, SALT);
final Cipher cipher = new Cipher(CipherFunction.AES_128_CTR, IV);
final Bytes48 publicKey = keyPair.getPublicKey().toBytesCompressed();
final KeyStoreData keyStoreData = KeyStore.encrypt(keyPair.getSecretKey().toBytes(), publicKey, password, "", kdfParam, cipher);
try {
KeyStoreLoader.saveToFile(keystoreDir.resolve(publicKey + ".json"), keyStoreData);
publicKey.toHexString();
} catch (IOException e) {
throw new IllegalStateException("Unable to create keystore file", e);
}
}
Aggregations