Skip to content

Commit

Permalink
fix: correct handling of legacy signatures when encoding (#1510)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestwich committed Oct 19, 2024
1 parent ee8b7a8 commit cc6f534
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ use alloy_primitives::{keccak256, Bytes, ChainId, Parity, Signature, TxKind, B25
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};
use core::mem;

/// Enforce correct parity for legacy transactions (EIP-155, 27 or 28).
macro_rules! legacy_sig {
($signature:expr) => {
if let Parity::Parity(parity) = $signature.v() {
&$signature.with_parity(Parity::NonEip155(parity))
} else {
$signature
}
};
}

/// Legacy transaction.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
Expand Down Expand Up @@ -124,6 +135,21 @@ impl RlpEcdsaTx for TxLegacy {
self.input.0.encode(out);
}

fn rlp_encoded_length_with_signature(&self, signature: &Signature) -> usize {
// Enforce correct parity for legacy transactions (EIP-155, 27 or 28).
let signature = legacy_sig!(signature);
let header = self.rlp_header_signed(&signature);
header.length() + header.payload_length
}

fn rlp_encode_signed(&self, signature: &Signature, out: &mut dyn BufMut) {
// Enforce correct parity for legacy transactions (EIP-155, 27 or 28).
let signature = legacy_sig!(signature);
self.rlp_header_signed(signature).encode(out);
self.rlp_encode_fields(out);
signature.write_rlp_vrs(out);
}

fn eip2718_encoded_length(&self, signature: &Signature) -> usize {
self.rlp_encoded_length_with_signature(signature)
}
Expand Down

0 comments on commit cc6f534

Please sign in to comment.