Skip to content

Commit

Permalink
fix: enforce correct parity encoding for typed transactions (alloy-rs…
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr authored and lwedge99 committed Oct 8, 2024
1 parent 5c019f1 commit 5ba45b4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
6 changes: 5 additions & 1 deletion crates/consensus/src/transaction/eip1559.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloc::vec::Vec;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{keccak256, Bytes, ChainId, Parity, Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -212,6 +212,10 @@ impl TxEip1559 {
let tx = Self::decode_fields(buf)?;
let signature = Signature::decode_rlp_vrs(buf)?;

if !matches!(signature.v(), Parity::Parity(_)) {
return Err(alloy_rlp::Error::Custom("invalid parity for typed transaction"));
}

let signed = tx.into_signed(signature);
if buf.len() + header.payload_length != original_len {
return Err(alloy_rlp::Error::ListLengthMismatch {
Expand Down
6 changes: 5 additions & 1 deletion crates/consensus/src/transaction/eip2930.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
use alloc::vec::Vec;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{keccak256, Bytes, ChainId, Parity, Signature, TxKind, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -205,6 +205,10 @@ impl TxEip2930 {
let tx = Self::decode_fields(buf)?;
let signature = Signature::decode_rlp_vrs(buf)?;

if !matches!(signature.v(), Parity::Parity(_)) {
return Err(alloy_rlp::Error::Custom("invalid parity for typed transaction"));
}

let signed = tx.into_signed(signature);
if buf.len() + header.payload_length != original_len {
return Err(alloy_rlp::Error::ListLengthMismatch {
Expand Down
6 changes: 5 additions & 1 deletion crates/consensus/src/transaction/eip4844.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType

use alloc::vec::Vec;
use alloy_eips::{eip2930::AccessList, eip4844::DATA_GAS_PER_BLOB, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Parity, Signature, TxKind, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -615,6 +615,10 @@ impl TxEip4844 {
let tx = Self::decode_fields(buf)?;
let signature = Signature::decode_rlp_vrs(buf)?;

if !matches!(signature.v(), Parity::Parity(_)) {
return Err(alloy_rlp::Error::Custom("invalid parity for typed transaction"));
}

let signed = tx.into_signed(signature);
if buf.len() + header.payload_length != original_len {
return Err(alloy_rlp::Error::ListLengthMismatch {
Expand Down
6 changes: 5 additions & 1 deletion crates/consensus/src/transaction/eip7702.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use alloy_eips::{
eip2930::AccessList,
eip7702::{constants::EIP7702_TX_TYPE_ID, SignedAuthorization},
};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{keccak256, Address, Bytes, ChainId, Parity, Signature, TxKind, B256, U256};
use alloy_rlp::{BufMut, Decodable, Encodable, Header};
use core::mem;

Expand Down Expand Up @@ -221,6 +221,10 @@ impl TxEip7702 {
let tx = Self::decode_fields(buf)?;
let signature = Signature::decode_rlp_vrs(buf)?;

if !matches!(signature.v(), Parity::Parity(_)) {
return Err(alloy_rlp::Error::Custom("invalid parity for typed transaction"));
}

let signed = tx.into_signed(signature);
if buf.len() + header.payload_length != original_len {
return Err(alloy_rlp::Error::ListLengthMismatch {
Expand Down
6 changes: 5 additions & 1 deletion crates/consensus/src/transaction/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::mem;

use alloc::vec::Vec;
use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization};
use alloy_primitives::{keccak256, Bytes, ChainId, Signature, TxKind, B256, U256};
use alloy_primitives::{keccak256, Bytes, ChainId, Parity, Signature, TxKind, B256, U256};
use alloy_rlp::{length_of_length, BufMut, Decodable, Encodable, Header, Result};

use crate::{EncodableSignature, SignableTransaction, Signed, Transaction, TxType};
Expand Down Expand Up @@ -171,6 +171,10 @@ impl TxLegacy {
let mut tx = Self::decode_fields(buf)?;
let signature = Signature::decode_rlp_vrs(buf)?;

if !matches!(signature.v(), Parity::Eip155(_) | Parity::NonEip155(_)) {
return Err(alloy_rlp::Error::Custom("invalid parity for legacy transaction"));
}

// extract chain id from signature
let v = signature.v();
tx.chain_id = v.chain_id();
Expand Down

0 comments on commit 5ba45b4

Please sign in to comment.