Class EvmTransaction

java.lang.Object
com.codename1.crypto.evm.EvmTransaction

public final class EvmTransaction extends Object

An EVM transaction in any of the three envelopes nodes accept: legacy (with EIP-155 replay protection), EIP-2930 access-list (type 1) and EIP-1559 dynamic-fee (type 2).

Built, signed and serialized locally, so a node receives finished bytes and never a key:

long nonce = 0;
UInt256 tip = UInt256.parseUnits("1", 9);           // 1 gwei
UInt256 maxFee = UInt256.parseUnits("30", 9);
byte[] privateKey = SecureRandom.bytes(32);
EvmTransaction tx = EvmTransaction.eip1559(1, nonce, tip, maxFee, 21000,
        EvmAddress.parse("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"), UInt256.parseUnits("0.01", 18), null);
byte[] raw = tx.sign(privateKey).encode();   // eth_sendRawTransaction

Instances are immutable; sign(byte[]) and withSignature(byte[]) answer a signed copy, and decode(byte[]) reads any signed or unsigned envelope back, recovering its sender().

  • Field Details

    • LEGACY

      public static final int LEGACY
      The legacy envelope; signed under EIP-155 when the chain id is not zero.
      See Also:
    • ACCESS_LIST

      public static final int ACCESS_LIST
      The EIP-2930 envelope, a legacy gas price plus an access list.
      See Also:
    • DYNAMIC_FEE

      public static final int DYNAMIC_FEE
      The EIP-1559 envelope with a priority fee and a fee cap.
      See Also:
  • Method Details

    • legacy

      public static EvmTransaction legacy(long chainId, long nonce, UInt256 gasPrice, long gasLimit, EvmAddress to, UInt256 value, byte[] data)

      A legacy transaction. A chainId of 0 signs without EIP-155 replay protection, which nodes increasingly refuse; pass the real chain id.

      Parameters
      • to: the recipient, or null to deploy data as a contract
    • accessList

      public static EvmTransaction accessList(long chainId, long nonce, UInt256 gasPrice, long gasLimit, EvmAddress to, UInt256 value, byte[] data, List<EvmTransaction.AccessEntry> accessList)
      An EIP-2930 access-list transaction.
    • eip1559

      public static EvmTransaction eip1559(long chainId, long nonce, UInt256 maxPriorityFeePerGas, UInt256 maxFeePerGas, long gasLimit, EvmAddress to, UInt256 value, byte[] data)

      An EIP-1559 transaction, the default on every chain that has it.

      Parameters
      • maxPriorityFeePerGas: the tip to the block producer, in wei per gas

      • maxFeePerGas: the most the sender pays per gas, base fee included

      • to: the recipient, or null to deploy data as a contract

    • withAccessList

      public EvmTransaction withAccessList(List<EvmTransaction.AccessEntry> entries)
      This transaction with an access list (types 1 and 2 only).
    • signingHash

      public byte[] signingHash()
      The 32-byte Keccak hash a signer signs.
    • sign

      public EvmTransaction sign(byte[] privateKey)
      Signs with a secp256k1 private key and answers the signed transaction.
    • withSignature

      public EvmTransaction withSignature(byte[] signature65)
      The signed transaction, given the 65-byte r || s || recoveryId signature of signingHash() that an external signer (hardware wallet, HSM) produced.
    • isSigned

      public boolean isSigned()
      Tells whether the transaction carries a signature.
    • encode

      public byte[] encode()
      The broadcastable bytes of a signed transaction, for eth_sendRawTransaction.
    • encodeHex

      public String encodeHex()
      encode() as 0x hex.
    • hash

      public byte[] hash()
      The transaction hash nodes and explorers know a signed transaction by.
    • sender

      public EvmAddress sender()

      The address that signed the transaction, recovered from the signature.

      Throws
      • IllegalStateException: when unsigned

      • com.codename1.security.CryptoException: when the signature recovers no key

    • maxCost

      public UInt256 maxCost()
      The most this transaction can cost: value + gasLimit * (maxFeePerGas or gasPrice).
    • decode

      public static EvmTransaction decode(byte[] raw)

      Reads a transaction in any envelope, signed or as the unsigned payload a wallet is asked to sign.

      Throws
      • IllegalArgumentException: for malformed or non-canonical bytes, or an envelope other than 0, 1 and 2
    • decodeHex

      public static EvmTransaction decodeHex(String hex)
      decode(byte[]) from 0x hex.
    • getType

      public int getType()
      The envelope: LEGACY, ACCESS_LIST or DYNAMIC_FEE.
    • getChainId

      public long getChainId()
      The chain id; 0 for a legacy transaction signed without EIP-155.
    • getNonce

      public long getNonce()
      The sender's nonce.
    • getGasPrice

      public UInt256 getGasPrice()
      The gas price of a legacy or access-list transaction; null for EIP-1559.
    • getMaxPriorityFeePerGas

      public UInt256 getMaxPriorityFeePerGas()
      The EIP-1559 tip; null for the other envelopes.
    • getMaxFeePerGas

      public UInt256 getMaxFeePerGas()
      The EIP-1559 fee cap; null for the other envelopes.
    • getGasLimit

      public long getGasLimit()
      The gas limit.
    • getTo

      public EvmAddress getTo()
      The recipient, or null for a contract deployment.
    • getValue

      public UInt256 getValue()
      The value sent, in wei.
    • getData

      public byte[] getData()
      A copy of the call data.
    • getAccessList

      public List<EvmTransaction.AccessEntry> getAccessList()
      The access list, empty for a legacy transaction.
    • getSignature

      public byte[] getSignature()
      The 65-byte r || s || recoveryId signature, or null when unsigned.