Search in sources :

Example 11 with KeyPair

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

the class TraceTransactionIntegrationTest method shouldTraceSStoreOperation.

@Test
public void shouldTraceSStoreOperation() {
    final KeyPair keyPair = SignatureAlgorithmFactory.getInstance().generateKeyPair();
    final Transaction createTransaction = Transaction.builder().type(TransactionType.FRONTIER).gasLimit(300_000).gasPrice(Wei.ZERO).nonce(0).payload(Bytes.fromHexString(CONTRACT_CREATION_DATA)).value(Wei.ZERO).signAndBuild(keyPair);
    final BlockHeader genesisBlockHeader = genesisBlock.getHeader();
    final MutableWorldState worldState = worldStateArchive.getMutable(genesisBlockHeader.getStateRoot(), genesisBlockHeader.getHash()).get();
    final WorldUpdater createTransactionUpdater = worldState.updater();
    TransactionProcessingResult result = transactionProcessor.processTransaction(blockchain, createTransactionUpdater, genesisBlockHeader, createTransaction, genesisBlockHeader.getCoinbase(), blockHashLookup, false, TransactionValidationParams.blockReplay());
    assertThat(result.isSuccessful()).isTrue();
    final Account createdContract = createTransactionUpdater.getTouchedAccounts().stream().filter(account -> !account.getCode().isEmpty()).findAny().get();
    createTransactionUpdater.commit();
    // Now call the transaction to execute the SSTORE.
    final DebugOperationTracer tracer = new DebugOperationTracer(new TraceOptions(true, true, true));
    final Transaction executeTransaction = Transaction.builder().type(TransactionType.FRONTIER).gasLimit(300_000).gasPrice(Wei.ZERO).nonce(1).payload(Bytes.fromHexString(CALL_SET_OTHER)).to(createdContract.getAddress()).value(Wei.ZERO).signAndBuild(keyPair);
    final WorldUpdater storeUpdater = worldState.updater();
    result = transactionProcessor.processTransaction(blockchain, storeUpdater, genesisBlockHeader, executeTransaction, genesisBlockHeader.getCoinbase(), tracer, blockHashLookup, false);
    assertThat(result.isSuccessful()).isTrue();
    // No storage changes before the SSTORE call.
    TraceFrame frame = tracer.getTraceFrames().get(170);
    assertThat(frame.getOpcode()).isEqualTo("DUP6");
    assertStorageContainsExactly(frame);
    // Storage changes show up in the SSTORE frame.
    frame = tracer.getTraceFrames().get(171);
    assertThat(frame.getOpcode()).isEqualTo("SSTORE");
    assertStorageContainsExactly(frame, entry("0x01", "0x6261720000000000000000000000000000000000000000000000000000000006"));
    // And storage changes are still present in future frames.
    frame = tracer.getTraceFrames().get(172);
    assertThat(frame.getOpcode()).isEqualTo("PUSH2");
    assertStorageContainsExactly(frame, entry("0x01", "0x6261720000000000000000000000000000000000000000000000000000000006"));
}
Also used : Account(org.hyperledger.besu.evm.account.Account) KeyPair(org.hyperledger.besu.crypto.KeyPair) Transaction(org.hyperledger.besu.ethereum.core.Transaction) MutableWorldState(org.hyperledger.besu.ethereum.core.MutableWorldState) WorldUpdater(org.hyperledger.besu.evm.worldstate.WorldUpdater) TraceOptions(org.hyperledger.besu.ethereum.debug.TraceOptions) BlockHeader(org.hyperledger.besu.ethereum.core.BlockHeader) TraceFrame(org.hyperledger.besu.ethereum.debug.TraceFrame) TransactionProcessingResult(org.hyperledger.besu.ethereum.processing.TransactionProcessingResult) Test(org.junit.jupiter.api.Test)

Example 12 with KeyPair

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

the class PeerDiscoveryAgentTest method testNodeRecordCreated.

@Test
public void testNodeRecordCreated() {
    KeyPair keyPair = SIGNATURE_ALGORITHM.get().createKeyPair(SIGNATURE_ALGORITHM.get().createPrivateKey(Bytes32.fromHexString("0xb71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")));
    final MockPeerDiscoveryAgent agent = helper.startDiscoveryAgent(helper.agentBuilder().nodeKey(NodeKeyUtils.createFrom(keyPair)).advertisedHost("127.0.0.1").bindPort(30303));
    assertThat(agent.getAdvertisedPeer().isPresent()).isTrue();
    assertThat(agent.getAdvertisedPeer().get().getNodeRecord().isPresent()).isTrue();
    final NodeRecord nodeRecord = agent.getAdvertisedPeer().get().getNodeRecord().get();
    assertThat(nodeRecord.getNodeId()).isNotNull();
    assertThat(nodeRecord.getIdentityScheme()).isNotNull();
    assertThat(nodeRecord.getSignature()).isNotNull();
    assertThat(nodeRecord.getSeq()).isNotNull();
    assertThat(nodeRecord.get("eth")).isNotNull();
    assertThat(nodeRecord.get("eth")).isEqualTo(Collections.singletonList(Collections.singletonList(Bytes.EMPTY)));
    assertThat(nodeRecord.asEnr()).isEqualTo("enr:-JC4QOfroMOa1sB6ajxcBKdWn3s9S4Ojl33pbRm72S5FnCwyZfskmjkJvZznQaWNTrOHrnKxw1R9xMm9rl" + "EGOcsOyscBg2V0aMLBgIJpZIJ2NIJpcIR_AAABiXNlY3AyNTZrMaEDymNMrg1JrLQB2KTGtv6MVbcNEV" + "v0AHacwUAPMljNMTiDdGNwAoN1ZHCCdl8");
}
Also used : NodeRecord(org.ethereum.beacon.discovery.schema.NodeRecord) KeyPair(org.hyperledger.besu.crypto.KeyPair) MockPeerDiscoveryAgent(org.hyperledger.besu.ethereum.p2p.discovery.internal.MockPeerDiscoveryAgent) Test(org.junit.Test)

Example 13 with KeyPair

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

the class BootNodesGenesisSetupTest method setUp.

@Before
public void setUp() throws Exception {
    int nodeAP2pBindingPort;
    int nodeBP2pBindingPort;
    try (ServerSocket nodeASocket = new ServerSocket(0);
        ServerSocket nodeBSocket = new ServerSocket(0)) {
        nodeAP2pBindingPort = nodeASocket.getLocalPort();
        nodeBP2pBindingPort = nodeBSocket.getLocalPort();
    }
    final KeyPair nodeAKeyPair = createKeyPair(Bytes32.fromHexString("0x8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63"));
    final KeyPair nodeBKeyPair = createKeyPair(Bytes32.fromHexString("0xc87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3"));
    nodeA = besu.createNode("nodeA", nodeBuilder -> configureNode(nodeBuilder, nodeAP2pBindingPort, nodeAKeyPair, nodeBKeyPair.getPublicKey(), nodeBP2pBindingPort));
    nodeB = besu.createNode("nodeB", nodeBuilder -> configureNode(nodeBuilder, nodeBP2pBindingPort, nodeBKeyPair, nodeAKeyPair.getPublicKey(), nodeAP2pBindingPort));
    cluster.start(nodeA, nodeB);
}
Also used : SECPPublicKey(org.hyperledger.besu.crypto.SECPPublicKey) AcceptanceTestBase(org.hyperledger.besu.tests.acceptance.dsl.AcceptanceTestBase) X9ECParameters(org.bouncycastle.asn1.x9.X9ECParameters) BeforeClass(org.junit.BeforeClass) BesuNodeConfigurationBuilder(org.hyperledger.besu.tests.acceptance.dsl.node.configuration.BesuNodeConfigurationBuilder) Test(org.junit.Test) SECPPrivateKey(org.hyperledger.besu.crypto.SECPPrivateKey) ServerSocket(java.net.ServerSocket) ECDomainParameters(org.bouncycastle.crypto.params.ECDomainParameters) SignatureAlgorithm(org.hyperledger.besu.crypto.SignatureAlgorithm) SECNamedCurves(org.bouncycastle.asn1.sec.SECNamedCurves) KeyPair(org.hyperledger.besu.crypto.KeyPair) Optional(java.util.Optional) Node(org.hyperledger.besu.tests.acceptance.dsl.node.Node) Bytes32(org.apache.tuweni.bytes.Bytes32) Before(org.junit.Before) KeyPair(org.hyperledger.besu.crypto.KeyPair) ServerSocket(java.net.ServerSocket) Before(org.junit.Before)

Example 14 with KeyPair

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

the class SECP256R1AcceptanceTest method setUp.

@Before
public void setUp() throws Exception {
    KeyPair minerNodeKeyPair = createKeyPair(MINER_NODE_PRIVATE_KEY);
    KeyPair otherNodeKeyPair = createKeyPair(OTHER_NODE_PRIVATE_KEY);
    final ClusterConfiguration clusterConfiguration = new ClusterConfigurationBuilder().awaitPeerDiscovery(false).build();
    noDiscoveryCluster = new Cluster(clusterConfiguration, net);
    minerNode = besu.createNodeWithNonDefaultSignatureAlgorithm("minerNode", GENESIS_FILE, minerNodeKeyPair);
    noDiscoveryCluster.start(minerNode);
    otherNode = besu.createNodeWithNonDefaultSignatureAlgorithm("otherNode", GENESIS_FILE, otherNodeKeyPair, List.of(minerNode));
    noDiscoveryCluster.addNode(otherNode);
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) ClusterConfigurationBuilder(org.hyperledger.besu.tests.acceptance.dsl.node.cluster.ClusterConfigurationBuilder) Cluster(org.hyperledger.besu.tests.acceptance.dsl.node.cluster.Cluster) ClusterConfiguration(org.hyperledger.besu.tests.acceptance.dsl.node.cluster.ClusterConfiguration) Before(org.junit.Before)

Example 15 with KeyPair

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

the class BlockDataGenerator method generateKeyPair.

public KeyPair generateKeyPair() {
    final java.security.KeyPair rawKeyPair = keyPairGenerator.generateKeyPair();
    final BCECPrivateKey privateKey = (BCECPrivateKey) rawKeyPair.getPrivate();
    final BCECPublicKey publicKey = (BCECPublicKey) rawKeyPair.getPublic();
    final BigInteger privateKeyValue = privateKey.getD();
    // Ethereum does not use encoded public keys like bitcoin - see
    // https://en.bitcoin.it/wiki/Elliptic_Curve_Digital_Signature_Algorithm for details
    // Additionally, as the first bit is a constant prefix (0x04) we ignore this value
    final byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
    final BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));
    return new KeyPair(signatureAlgorithm.createPrivateKey(privateKeyValue), signatureAlgorithm.createPublicKey(publicKeyValue));
}
Also used : KeyPair(org.hyperledger.besu.crypto.KeyPair) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) BigInteger(java.math.BigInteger) BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey)

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