Search in sources :

Example 1 with KeyPair

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

the class TransactionData method getSignedTransaction.

public Transaction getSignedTransaction(final NonceProvider nonceProvider) {
    final KeyPair keyPair = SIGNATURE_ALGORITHM.get().createKeyPair(privateKey);
    final Address fromAddress = Address.extract(keyPair.getPublicKey());
    final long nonce = nonceProvider.get(fromAddress);
    return Transaction.builder().gasLimit(gasLimit).gasPrice(gasPrice).nonce(nonce).payload(data).value(value).to(to.orElse(null)).guessType().signAndBuild(keyPair);
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) Address(org.hyperledger.besu.datatypes.Address)

Example 2 with KeyPair

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

the class GenerateBlockchainConfig method generateNodeKeypair.

/**
 * Generate a keypair for a node.
 *
 * @param node The number of the node.
 */
private void generateNodeKeypair(final int node) {
    try {
        LOG.info("Generating keypair for node {}.", node);
        final KeyPair keyPair = SIGNATURE_ALGORITHM.get().generateKeyPair();
        writeKeypair(keyPair.getPublicKey(), keyPair.getPrivateKey());
    } catch (final IOException e) {
        LOG.error("An error occurred while trying to generate node keypair.", e);
    }
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) IOException(java.io.IOException)

Example 3 with KeyPair

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

the class FixedKeySigningPrivateMarkerTransactionFactoryTest method createsFullyPopulatedPrivateMarkerTransactionUsingProvidedNonce.

@Test
public void createsFullyPopulatedPrivateMarkerTransactionUsingProvidedNonce() {
    final PrivateTransaction privTransaction = mock(PrivateTransaction.class);
    final Wei gasPrice = Wei.of(100);
    final long gasLimit = 500;
    final long providedNonce = 100;
    final String enclaveKey = "pmtLookupKey";
    final KeyPair signingKeys = SignatureAlgorithmFactory.getInstance().generateKeyPair();
    final Address precompiledAddress = Address.fromHexString("1");
    final FixedKeySigningPrivateMarkerTransactionFactory factory = new FixedKeySigningPrivateMarkerTransactionFactory(signingKeys);
    final Transaction unsignedPrivateMarkerTransaction = new Transaction.Builder().type(TransactionType.FRONTIER).nonce(providedNonce).gasPrice(gasPrice).gasLimit(gasLimit).to(precompiledAddress).value(Wei.ZERO).payload(Bytes.fromBase64String(enclaveKey)).build();
    final Transaction transaction = Transaction.readFrom(factory.create(unsignedPrivateMarkerTransaction, privTransaction, ""));
    assertThat(transaction.getNonce()).isEqualTo(providedNonce);
    assertThat(transaction.getGasLimit()).isEqualTo(gasLimit);
    assertThat(transaction.getGasPrice().get()).isEqualTo(gasPrice);
    assertThat(transaction.getValue()).isEqualTo(Wei.ZERO);
    assertThat(transaction.getSender()).isEqualTo(Util.publicKeyToAddress(signingKeys.getPublicKey()));
    assertThat(transaction.getTo()).isEqualTo(Optional.of(precompiledAddress));
    assertThat(transaction.getPayload()).isEqualTo(Bytes.fromBase64String(enclaveKey));
    final Transaction nextTransaction = Transaction.readFrom(factory.create(unsignedPrivateMarkerTransaction, privTransaction, ""));
    assertThat(nextTransaction.getSender()).isEqualTo(transaction.getSender());
}
Also used : PrivateTransaction(org.hyperledger.besu.ethereum.privacy.PrivateTransaction) KeyPair(org.hyperledger.besu.crypto.KeyPair) Address(org.hyperledger.besu.datatypes.Address) PrivateTransaction(org.hyperledger.besu.ethereum.privacy.PrivateTransaction) Transaction(org.hyperledger.besu.ethereum.core.Transaction) Wei(org.hyperledger.besu.datatypes.Wei) Test(org.junit.Test)

Example 4 with KeyPair

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

the class MainnetTransactionValidatorTest method transactionWithNullSenderCanBeValidIfGasPriceAndValueIsZero.

@Test
public void transactionWithNullSenderCanBeValidIfGasPriceAndValueIsZero() {
    final MainnetTransactionValidator validator = new MainnetTransactionValidator(gasCalculator, false, Optional.of(BigInteger.ONE), defaultGoQuorumCompatibilityMode);
    final TransactionTestFixture builder = new TransactionTestFixture();
    final KeyPair senderKeyPair = SIGNATURE_ALGORITHM.get().generateKeyPair();
    final Address arbitrarySender = Address.fromHexString("1");
    builder.gasPrice(Wei.ZERO).nonce(0).sender(arbitrarySender).value(Wei.ZERO);
    assertThat(validator.validateForSender(builder.createTransaction(senderKeyPair), null, false)).isEqualTo(ValidationResult.valid());
}
Also used : TransactionTestFixture(org.hyperledger.besu.ethereum.core.TransactionTestFixture) KeyPair(org.hyperledger.besu.crypto.KeyPair) Address(org.hyperledger.besu.datatypes.Address) Test(org.junit.Test)

Example 5 with KeyPair

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

the class CliqueExtraDataValidationRuleTest method signerNotInExpectedValidatorsFailsValidation.

@Test
public void signerNotInExpectedValidatorsFailsValidation() {
    final KeyPair otherSigner = SignatureAlgorithmFactory.getInstance().generateKeyPair();
    final BlockHeaderTestFixture headerBuilder = new BlockHeaderTestFixture();
    final BlockHeader parent = headerBuilder.number(1).buildHeader();
    headerBuilder.number(2);
    final BlockHeader badlySignedChild = TestHelpers.createCliqueSignedBlockHeader(headerBuilder, otherSigner, Lists.newArrayList());
    final CliqueExtraDataValidationRule rule = new CliqueExtraDataValidationRule(new EpochManager(10));
    assertThat(rule.validate(badlySignedChild, parent, cliqueProtocolContext)).isFalse();
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BlockHeaderTestFixture(org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) EpochManager(org.hyperledger.besu.consensus.common.EpochManager) 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