Search in sources :

Example 1 with Bytes48

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));
    }
}
Also used : ChainDataUnavailableException(tech.pegasys.teku.storage.client.ChainDataUnavailableException) Bytes48(org.apache.tuweni.bytes.Bytes48) BadRequestException(tech.pegasys.teku.api.exceptions.BadRequestException) UInt64(tech.pegasys.teku.infrastructure.unsigned.UInt64)

Example 2 with Bytes48

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);
}
Also used : Bytes48(org.apache.tuweni.bytes.Bytes48) Test(org.junit.jupiter.api.Test)

Example 3 with Bytes48

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);
}
Also used : Bytes48(org.apache.tuweni.bytes.Bytes48) Test(org.junit.jupiter.api.Test)

Example 4 with Bytes48

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);
}
Also used : Bytes48(org.apache.tuweni.bytes.Bytes48) Test(org.junit.jupiter.api.Test) AbstractPublicKeyTest(tech.pegasys.teku.bls.impl.AbstractPublicKeyTest)

Example 5 with Bytes48

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);
    }
}
Also used : Bytes48(org.apache.tuweni.bytes.Bytes48) KdfParam(tech.pegasys.signers.bls.keystore.model.KdfParam) Cipher(tech.pegasys.signers.bls.keystore.model.Cipher) IOException(java.io.IOException) KeyStoreData(tech.pegasys.signers.bls.keystore.model.KeyStoreData) Pbkdf2Param(tech.pegasys.signers.bls.keystore.model.Pbkdf2Param)

Aggregations

Bytes48 (org.apache.tuweni.bytes.Bytes48)7 Test (org.junit.jupiter.api.Test)5 AbstractPublicKeyTest (tech.pegasys.teku.bls.impl.AbstractPublicKeyTest)2 IOException (java.io.IOException)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 Bytes (org.apache.tuweni.bytes.Bytes)1 Bytes32 (org.apache.tuweni.bytes.Bytes32)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertFalse (org.junit.jupiter.api.Assertions.assertFalse)1 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 Cipher (tech.pegasys.signers.bls.keystore.model.Cipher)1 KdfParam (tech.pegasys.signers.bls.keystore.model.KdfParam)1 KeyStoreData (tech.pegasys.signers.bls.keystore.model.KeyStoreData)1