Search in sources :

Example 6 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class IbftBlockHeaderValidationRulesetFactoryTest method ibftValidateHeaderPasses.

@Test
public void ibftValidateHeaderPasses() {
    final KeyPair proposerKeyPair = SIGNATURE_ALGORITHM.get().generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = singletonList(proposerAddress);
    final BlockHeader parentHeader = buildBlockHeader(1, proposerKeyPair, validators, null);
    final BlockHeader blockHeader = buildBlockHeader(2, proposerKeyPair, validators, parentHeader);
    final BlockHeaderValidator validator = IbftBlockHeaderValidationRulesetFactory.ibftBlockHeaderValidator(5, 0).build();
    assertThat(validator.validateHeader(blockHeader, parentHeader, setupContextWithValidators(validators), HeaderValidationMode.FULL)).isTrue();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) Address(org.hyperledger.besu.datatypes.Address) BlockHeaderValidator(org.hyperledger.besu.ethereum.mainnet.BlockHeaderValidator) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 7 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method createExtraDataWithCommitSeals.

private IbftExtraData createExtraDataWithCommitSeals(final BlockHeader header, final Collection<KeyPair> committerKeyPairs) {
    final IbftExtraData extraDataInHeader = IbftExtraData.decode(header);
    final Hash headerHashForCommitters = IbftBlockHashing.calculateDataHashForCommittedSeal(header, extraDataInHeader);
    final List<SECPSignature> commitSeals = committerKeyPairs.stream().map(keys -> signatureAlgorithm.sign(headerHashForCommitters, keys)).collect(Collectors.toList());
    return new IbftExtraData(extraDataInHeader.getVanityData(), commitSeals, extraDataInHeader.getProposerSeal(), extraDataInHeader.getValidators());
}
Also used : IbftBlockHashing(org.hyperledger.besu.consensus.ibftlegacy.IbftBlockHashing) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) Collections.emptyList(java.util.Collections.emptyList) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Collection(java.util.Collection) AddressHelpers(org.hyperledger.besu.ethereum.core.AddressHelpers) Test(org.junit.Test) Bytes(org.apache.tuweni.bytes.Bytes) SignatureAlgorithmFactory(org.hyperledger.besu.crypto.SignatureAlgorithmFactory) Address(org.hyperledger.besu.datatypes.Address) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Collectors(java.util.stream.Collectors) Collections.singletonList(java.util.Collections.singletonList) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) List(java.util.List) Lists(com.google.common.collect.Lists) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) IbftLegacyContextBuilder.setupContextWithValidators(org.hyperledger.besu.consensus.ibftlegacy.IbftLegacyContextBuilder.setupContextWithValidators) KeyPair(org.hyperledger.besu.crypto.KeyPair) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) Assertions(org.assertj.core.api.Assertions) Collections(java.util.Collections) Hash(org.hyperledger.besu.datatypes.Hash) SECPSignature(org.hyperledger.besu.crypto.SECPSignature) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) Hash(org.hyperledger.besu.datatypes.Hash)

Example 8 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method proposerNotInValidatorListFailsValidation.

@Test
public void proposerNotInValidatorListFailsValidation() {
    final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
    // must NOT be block 0, as that should not contain seals at all
    builder.number(1);
    final KeyPair proposerKeyPair = signatureAlgorithm.generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = Lists.newArrayList(AddressHelpers.calculateAddressWithRespectTo(proposerAddress, 1), proposerAddress);
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, 0);
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    // Insert an extraData block with committer seals.
    final IbftExtraData commitedExtraData = createExtraDataWithCommitSeals(header, singletonList(proposerKeyPair));
    builder.extraData(commitedExtraData.encode());
    header = builder.buildHeader();
    assertThat(extraDataValidationRule.validate(header, null, context)).isFalse();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) Test(org.junit.Test)

Example 9 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class BftExtraDataValidationRuleTest method subExecution.

private boolean subExecution(final int validatorCount, final int committerCount, final boolean useTwoThirds) {
    final BlockHeaderTestFixture builder = new BlockHeaderTestFixture();
    // must NOT be block 0, as that should not contain seals at all
    builder.number(1);
    final KeyPair proposerKeyPair = signatureAlgorithm.generateKeyPair();
    final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes()));
    final List<Address> validators = Lists.newArrayList();
    final List<KeyPair> committerKeys = Lists.newArrayList();
    validators.add(proposerAddress);
    committerKeys.add(proposerKeyPair);
    for (int i = 0; i < validatorCount - 1; i++) {
        // need -1 to account for proposer
        final KeyPair committerKeyPair = signatureAlgorithm.generateKeyPair();
        committerKeys.add(committerKeyPair);
        validators.add(Address.extract(Hash.hash(committerKeyPair.getPublicKey().getEncodedBytes())));
    }
    Collections.sort(validators);
    BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators);
    final IbftExtraData commitedExtraData = createExtraDataWithCommitSeals(header, committerKeys.subList(0, committerCount));
    builder.extraData(commitedExtraData.encode());
    header = builder.buildHeader();
    final ProtocolContext context = new ProtocolContext(null, null, setupContextWithValidators(validators));
    final IbftExtraDataValidationRule extraDataValidationRule = new IbftExtraDataValidationRule(true, useTwoThirds ? 0 : 2);
    return extraDataValidationRule.validate(header, null, context);
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) Address(org.hyperledger.besu.datatypes.Address) IbftExtraData(org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader)

Example 10 with KeyPair

use of org.hyperledger.besu.crypto.KeyPair in project besu by hyperledger.

the class PublicKeySubCommandTest method callingPublicKeyExportAddressSubCommandWithPrivateKeyFileMustWriteKeyToStandardOutput.

@Test
public void callingPublicKeyExportAddressSubCommandWithPrivateKeyFileMustWriteKeyToStandardOutput() throws IOException {
    final SECPPrivateKey privateKey = SECPPrivateKey.create(Bytes32.fromHexString("0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"), ALGORITHM);
    final KeyPair keyPair = KeyPair.create(privateKey, curve, ALGORITHM);
    final Path privateKeyFile = Files.createTempFile("private", "address");
    Files.writeString(privateKeyFile, privateKey.toString());
    parseCommand(PUBLIC_KEY_SUBCOMMAND_NAME, PUBLIC_KEY_EXPORT_ADDRESS_SUBCOMMAND_NAME, "--node-private-key-file", privateKeyFile.toString());
    final String expectedOutputStart = Util.publicKeyToAddress(keyPair.getPublicKey()).toString();
    assertThat(commandOutput.toString(UTF_8)).startsWith(expectedOutputStart);
    assertThat(commandErrorOutput.toString(UTF_8)).isEmpty();
}
Also used : Path(java.nio.file.Path) KeyPair(org.hyperledger.besu.crypto.KeyPair) SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) Test(org.junit.Test)

Aggregations

KeyPair (org.hyperledger.besu.crypto.KeyPair)34 Test (org.junit.Test)21 Address (org.hyperledger.besu.datatypes.Address)14 BlockHeader (org.hyperledger.besu.ethereum.core.BlockHeader)13 BlockHeaderTestFixture (org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture)10 IbftExtraData (org.hyperledger.besu.consensus.ibftlegacy.IbftExtraData)9 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)9 Transaction (org.hyperledger.besu.ethereum.core.Transaction)7 SignatureAlgorithm (org.hyperledger.besu.crypto.SignatureAlgorithm)5 ArrayList (java.util.ArrayList)4 SECPPrivateKey (org.hyperledger.besu.crypto.SECPPrivateKey)4 BigInteger (java.math.BigInteger)3 Bytes (org.apache.tuweni.bytes.Bytes)3 Path (java.nio.file.Path)2 Collection (java.util.Collection)2 Bytes32 (org.apache.tuweni.bytes.Bytes32)2 NodeRecord (org.ethereum.beacon.discovery.schema.NodeRecord)2 SECPSignature (org.hyperledger.besu.crypto.SECPSignature)2 Hash (org.hyperledger.besu.datatypes.Hash)2 BlockWithMetadata (org.hyperledger.besu.ethereum.api.query.BlockWithMetadata)2