Skip to content

Commit

Permalink
solution: open access to the legacy tx encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
splix committed Feb 2, 2025
1 parent 0e7371b commit 7fb9f1f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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())
Expand Down

0 comments on commit 7fb9f1f

Please sign in to comment.