Skip to content

Commit 7fb9f1f

Browse files
committed
solution: open access to the legacy tx encoder
1 parent 0e7371b commit 7fb9f1f

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/Transaction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public byte[] hash() {
152152
}
153153

154154
public byte[] hash(Integer chainId) {
155-
byte[] rlp = TransactionEncoder.DEFAULT.encodeStandard(this, false, chainId);
155+
byte[] rlp = TransactionEncoder.DEFAULT.encodeLegacy(this, false, chainId);
156156

157157
Keccak.Digest256 keccak = new Keccak.Digest256();
158158
keccak.update(rlp);

etherjar-tx/src/main/java/io/emeraldpay/etherjar/tx/TransactionEncoder.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public byte[] encode(Transaction tx, boolean includeSignature) {
3737
}
3838
if (tx.getType() == TransactionType.STANDARD) {
3939
if (includeSignature) {
40-
return encodeStandard(tx, true, null);
40+
return encodeLegacy(tx, true, null);
4141
} else {
4242
Signature signature = tx.getSignature();
4343
if (signature.getType() == SignatureType.EIP155) {
4444
int chainId = ((SignatureEIP155)signature).getChainId();
45-
return encodeStandard(tx, false, chainId);
45+
return encodeLegacy(tx, false, chainId);
4646
} else {
4747
throw new IllegalStateException("Neither signature nor chainId specified");
4848
}
@@ -51,7 +51,16 @@ public byte[] encode(Transaction tx, boolean includeSignature) {
5151
throw new IllegalStateException("Unsupported transaction type: " + tx.getType());
5252
}
5353

54-
protected byte[] encodeStandard(Transaction tx, boolean includeSignature, Integer chainId) {
54+
/**
55+
* Encode a Legacy transaction (pre EIP-2718 which introduces a new RLP format).
56+
* For legacy transaction you have to encode either the signature or the chainId for unsigned transaction.
57+
*
58+
* @param tx legacy transaction
59+
* @param includeSignature if true, include signature, if false include chainId
60+
* @param chainId chain id to include in the transaction if signature is not specified
61+
* @return RLP encoded transaction
62+
*/
63+
public byte[] encodeLegacy(Transaction tx, boolean includeSignature, Integer chainId) {
5564
RlpWriter wrt = new RlpWriter();
5665
wrt.startList()
5766
.write(tx.getNonce())

0 commit comments

Comments
 (0)