Search in sources :

Example 1 with ImmutableTransaction

use of org.ethereum.core.ImmutableTransaction in project rskj by rsksmart.

the class TransactionsMessage method parse.

private void parse() {
    RLPList paramsList = (RLPList) RLP.decode2(encoded).get(0);
    transactions = new ArrayList<>();
    for (int i = 0; i < paramsList.size(); ++i) {
        RLPList rlpTxData = (RLPList) paramsList.get(i);
        Transaction tx = new ImmutableTransaction(rlpTxData.getRLPData());
        transactions.add(tx);
    }
    parsed = true;
}
Also used : ImmutableTransaction(org.ethereum.core.ImmutableTransaction) Transaction(org.ethereum.core.Transaction) ImmutableTransaction(org.ethereum.core.ImmutableTransaction) RLPList(org.ethereum.util.RLPList)

Example 2 with ImmutableTransaction

use of org.ethereum.core.ImmutableTransaction in project rskj by rsksmart.

the class Secp256k1ServiceTest method testSignatureToKey_from_Tx.

@Test
public void testSignatureToKey_from_Tx() throws SignatureException {
    byte[] messageHash = HashUtil.keccak256(exampleMessage.getBytes());
    byte[] pk = this.privateKey.toByteArray();
    String receiver = "CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826";
    ECKey fromPrivate = ECKey.fromPrivate(pk);
    ECKey fromPrivateDecompress = fromPrivate.decompress();
    String pubKeyExpected = Hex.toHexString(fromPrivateDecompress.getPubKey());
    String addressExpected = Hex.toHexString(fromPrivateDecompress.getAddress());
    // Create tx and sign, then recover from serialized.
    Transaction newTx = Transaction.builder().nonce(BigInteger.valueOf(2L)).gasPrice(BigInteger.valueOf(2L)).gasLimit(BigInteger.valueOf(2L)).destination(Hex.decode(receiver)).data(messageHash).value(BigInteger.valueOf(2L)).build();
    newTx.sign(pk);
    ImmutableTransaction recoveredTx = new ImmutableTransaction(newTx.getEncoded());
    // Recover Pub Key from recovered tx
    ECKey actualKey = this.getSecp256k1().signatureToKey(HashUtil.keccak256(recoveredTx.getEncodedRaw()), recoveredTx.getSignature());
    // Recover PK and Address.
    String pubKeyActual = Hex.toHexString(actualKey.getPubKey());
    logger.debug("Signature public key\t: {}", pubKeyActual);
    assertEquals(pubKeyExpected, pubKeyActual);
    assertEquals(pubString, pubKeyActual);
    assertArrayEquals(pubKey, actualKey.getPubKey());
    String addressActual = Hex.toHexString(actualKey.getAddress());
    logger.debug("Sender is\t\t: {}", addressActual);
    assertEquals(addressExpected, addressActual);
}
Also used : ImmutableTransaction(org.ethereum.core.ImmutableTransaction) Transaction(org.ethereum.core.Transaction) ImmutableTransaction(org.ethereum.core.ImmutableTransaction) ECKey(org.ethereum.crypto.ECKey) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 3 with ImmutableTransaction

use of org.ethereum.core.ImmutableTransaction in project rskj by rsksmart.

the class FreeBlock method parseTxs.

private static List<Transaction> parseTxs(RLPList txTransactions) {
    List<Transaction> parsedTxs = new ArrayList<>();
    for (int i = 0; i < txTransactions.size(); i++) {
        RLPElement transactionRaw = txTransactions.get(i);
        Transaction tx = new ImmutableTransaction(transactionRaw.getRLPData());
        parsedTxs.add(tx);
    }
    return Collections.unmodifiableList(parsedTxs);
}
Also used : ImmutableTransaction(org.ethereum.core.ImmutableTransaction) Transaction(org.ethereum.core.Transaction) RLPElement(org.ethereum.util.RLPElement) ImmutableTransaction(org.ethereum.core.ImmutableTransaction) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 4 with ImmutableTransaction

use of org.ethereum.core.ImmutableTransaction in project rskj by rsksmart.

the class ECKeyTest method testVerifySignature3.

@Test
@Ignore("The TX sender is not 20-byte long")
public void testVerifySignature3() throws SignatureException {
    byte[] rawtx = Hex.decode("f86e80893635c9adc5dea000008609184e72a00082109f9479b08ad8787060333663d19704909ee7b1903e58801ba0899b92d0c76cbf18df24394996beef19c050baa9823b4a9828cd9b260c97112ea0c9e62eb4cf0a9d95ca35c8830afac567619d6b3ebee841a3c8be61d35acd8049");
    Transaction tx = new ImmutableTransaction(rawtx);
    ECKey key = ECKey.signatureToKey(HashUtil.keccak256(rawtx), tx.getSignature().toBase64());
    System.out.println("Signature public key\t: " + Hex.toHexString(key.getPubKey()));
    System.out.println("Sender is\t\t: " + Hex.toHexString(key.getAddress()));
// sender: CD2A3D9F938E13CD947EC05ABC7FE734DF8DD826
// todo: add test assertion when the sign/verify part actually works.
}
Also used : ImmutableTransaction(org.ethereum.core.ImmutableTransaction) Transaction(org.ethereum.core.Transaction) ImmutableTransaction(org.ethereum.core.ImmutableTransaction) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 5 with ImmutableTransaction

use of org.ethereum.core.ImmutableTransaction in project rskj by rsksmart.

the class TransactionBuilder method build.

private Transaction build(String to, BigInteger nonce, BigInteger gasLimit, BigInteger gasPrice, byte chainId, byte[] data, BigInteger value, byte[] privKeyBytes, boolean immutable) {
    Transaction tx = Transaction.builder().destination(to).nonce(nonce).gasLimit(gasLimit).gasPrice(gasPrice).chainId(chainId).data(data).value(value).build();
    tx.sign(privKeyBytes);
    if (immutable) {
        return new ImmutableTransaction(tx.getEncoded());
    }
    return tx;
}
Also used : ImmutableTransaction(org.ethereum.core.ImmutableTransaction) Transaction(org.ethereum.core.Transaction) ImmutableTransaction(org.ethereum.core.ImmutableTransaction)

Aggregations

ImmutableTransaction (org.ethereum.core.ImmutableTransaction)6 Transaction (org.ethereum.core.Transaction)6 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 TransactionPoolAddResult (org.ethereum.core.TransactionPoolAddResult)1 ECKey (org.ethereum.crypto.ECKey)1 RLPElement (org.ethereum.util.RLPElement)1 RLPList (org.ethereum.util.RLPList)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Ignore (org.junit.Ignore)1