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();
}
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());
}
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();
}
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);
}
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();
}
Aggregations