Skip to content

Commit

Permalink
feat(consensus): Protected Legacy Signature (#1578)
Browse files Browse the repository at this point in the history
feat: protected tx signature
  • Loading branch information
refcell authored Oct 28, 2024
1 parent c4f3543 commit b227b17
Showing 1 changed file with 63 additions and 2 deletions.
65 changes: 63 additions & 2 deletions crates/consensus/src/transaction/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ impl TxEnvelope {
matches!(self, Self::Eip7702(_))
}

/// Returns true if the signature of the transaction is protected.
#[inline]
pub const fn is_protected(&self) -> bool {
match self {
Self::Legacy(tx) => {
let v = tx.signature().v().to_u64();
if 64 - v.leading_zeros() <= 8 {
return v != 27 && v != 28 && v != 1 && v != 0;
}
// anything not 27 or 28 is considered protected
true
}
_ => true,
}
}

/// Returns the [`TxLegacy`] variant if the transaction is a legacy transaction.
pub const fn as_legacy(&self) -> Option<&Signed<TxLegacy>> {
match self {
Expand Down Expand Up @@ -588,9 +604,9 @@ mod tests {
eip4844::BlobTransactionSidecar,
eip7702::Authorization,
};
use alloy_primitives::{hex, Address, Parity, Signature, U256};
#[allow(unused_imports)]
use alloy_primitives::{Bytes, TxKind};
use alloy_primitives::{b256, Bytes, TxKind};
use alloy_primitives::{hex, Address, Parity, Signature, U256};
use std::{fs, path::PathBuf, str::FromStr, vec};

#[cfg(not(feature = "std"))]
Expand All @@ -617,6 +633,51 @@ mod tests {
assert_eq!(from, address!("001e2b7dE757bA469a57bF6b23d982458a07eFcE"));
}

#[test]
fn test_is_protected_v() {
let sig = Signature::test_signature();
assert!(!&TxEnvelope::Legacy(Signed::new_unchecked(
TxLegacy::default(),
sig,
Default::default(),
))
.is_protected());
let r = b256!("840cfc572845f5786e702984c2a582528cad4b49b2a10b9db1be7fca90058565");
let s = b256!("25e7109ceb98168d95b09b18bbf6b685130e0562f233877d492b94eee0c5b6d1");
let v = 27;
let valid_sig = Signature::from_scalars_and_parity(r, s, v).unwrap();
assert!(!&TxEnvelope::Legacy(Signed::new_unchecked(
TxLegacy::default(),
valid_sig,
Default::default(),
))
.is_protected());
assert!(&TxEnvelope::Eip2930(Signed::new_unchecked(
TxEip2930::default(),
sig,
Default::default(),
))
.is_protected());
assert!(&TxEnvelope::Eip1559(Signed::new_unchecked(
TxEip1559::default(),
sig,
Default::default(),
))
.is_protected());
assert!(&TxEnvelope::Eip4844(Signed::new_unchecked(
TxEip4844Variant::TxEip4844(TxEip4844::default()),
sig,
Default::default(),
))
.is_protected());
assert!(&TxEnvelope::Eip7702(Signed::new_unchecked(
TxEip7702::default(),
sig,
Default::default(),
))
.is_protected());
}

#[test]
#[cfg(feature = "k256")]
// Test vector from https://etherscan.io/tx/0x280cde7cdefe4b188750e76c888f13bd05ce9a4d7767730feefe8a0e50ca6fc4
Expand Down

0 comments on commit b227b17

Please sign in to comment.