From 7fb9f1ff8c12eb9e15945e47635630490da53885 Mon Sep 17 00:00:00 2001 From: Igor Artamonov Date: Sun, 2 Feb 2025 21:05:34 +0000 Subject: [PATCH] solution: open access to the legacy tx encoder --- .../io/emeraldpay/etherjar/tx/Transaction.java | 2 +- .../etherjar/tx/TransactionEncoder.java | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/Transaction.java b/etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/Transaction.java index 057ad3c..7b5d7f7 100644 --- a/etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/Transaction.java +++ b/etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/Transaction.java @@ -152,7 +152,7 @@ public byte[] hash() { } public byte[] hash(Integer chainId) { - byte[] rlp = TransactionEncoder.DEFAULT.encodeStandard(this, false, chainId); + byte[] rlp = TransactionEncoder.DEFAULT.encodeLegacy(this, false, chainId); Keccak.Digest256 keccak = new Keccak.Digest256(); keccak.update(rlp); diff --git a/etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/TransactionEncoder.java b/etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/TransactionEncoder.java index 8b1ac03..c68a00e 100644 --- a/etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/TransactionEncoder.java +++ b/etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/TransactionEncoder.java @@ -37,12 +37,12 @@ public byte[] encode(Transaction tx, boolean includeSignature) { } if (tx.getType() == TransactionType.STANDARD) { if (includeSignature) { - return encodeStandard(tx, true, null); + return encodeLegacy(tx, true, null); } else { Signature signature = tx.getSignature(); if (signature.getType() == SignatureType.EIP155) { int chainId = ((SignatureEIP155)signature).getChainId(); - return encodeStandard(tx, false, chainId); + return encodeLegacy(tx, false, chainId); } else { throw new IllegalStateException("Neither signature nor chainId specified"); } @@ -51,7 +51,16 @@ public byte[] encode(Transaction tx, boolean includeSignature) { throw new IllegalStateException("Unsupported transaction type: " + tx.getType()); } - protected byte[] encodeStandard(Transaction tx, boolean includeSignature, Integer chainId) { + /** + * Encode a Legacy transaction (pre EIP-2718 which introduces a new RLP format). + * For legacy transaction you have to encode either the signature or the chainId for unsigned transaction. + * + * @param tx legacy transaction + * @param includeSignature if true, include signature, if false include chainId + * @param chainId chain id to include in the transaction if signature is not specified + * @return RLP encoded transaction + */ + public byte[] encodeLegacy(Transaction tx, boolean includeSignature, Integer chainId) { RlpWriter wrt = new RlpWriter(); wrt.startList() .write(tx.getNonce())