refactor: Replace reth recover_singer_with_buf with alloy (#17107)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Roman Hodulák
2025-06-30 15:41:35 +02:00
committed by GitHub
parent 678b5cd1fc
commit c63459884e
7 changed files with 49 additions and 143 deletions

View File

@@ -476,33 +476,33 @@ alloy-trie = { version = "0.9.0", default-features = false }
alloy-hardforks = "0.2.7"
alloy-consensus = { version = "1.0.13", default-features = false }
alloy-contract = { version = "1.0.13", default-features = false }
alloy-eips = { version = "1.0.13", default-features = false }
alloy-genesis = { version = "1.0.13", default-features = false }
alloy-json-rpc = { version = "1.0.13", default-features = false }
alloy-network = { version = "1.0.13", default-features = false }
alloy-network-primitives = { version = "1.0.13", default-features = false }
alloy-provider = { version = "1.0.13", features = ["reqwest"], default-features = false }
alloy-pubsub = { version = "1.0.13", default-features = false }
alloy-rpc-client = { version = "1.0.13", default-features = false }
alloy-rpc-types = { version = "1.0.13", features = ["eth"], default-features = false }
alloy-rpc-types-admin = { version = "1.0.13", default-features = false }
alloy-rpc-types-anvil = { version = "1.0.13", default-features = false }
alloy-rpc-types-beacon = { version = "1.0.13", default-features = false }
alloy-rpc-types-debug = { version = "1.0.13", default-features = false }
alloy-rpc-types-engine = { version = "1.0.13", default-features = false }
alloy-rpc-types-eth = { version = "1.0.13", default-features = false }
alloy-rpc-types-mev = { version = "1.0.13", default-features = false }
alloy-rpc-types-trace = { version = "1.0.13", default-features = false }
alloy-rpc-types-txpool = { version = "1.0.13", default-features = false }
alloy-serde = { version = "1.0.13", default-features = false }
alloy-signer = { version = "1.0.13", default-features = false }
alloy-signer-local = { version = "1.0.13", default-features = false }
alloy-transport = { version = "1.0.13" }
alloy-transport-http = { version = "1.0.13", features = ["reqwest-rustls-tls"], default-features = false }
alloy-transport-ipc = { version = "1.0.13", default-features = false }
alloy-transport-ws = { version = "1.0.13", default-features = false }
alloy-consensus = { version = "1.0.16", default-features = false }
alloy-contract = { version = "1.0.16", default-features = false }
alloy-eips = { version = "1.0.16", default-features = false }
alloy-genesis = { version = "1.0.16", default-features = false }
alloy-json-rpc = { version = "1.0.16", default-features = false }
alloy-network = { version = "1.0.16", default-features = false }
alloy-network-primitives = { version = "1.0.16", default-features = false }
alloy-provider = { version = "1.0.16", features = ["reqwest"], default-features = false }
alloy-pubsub = { version = "1.0.16", default-features = false }
alloy-rpc-client = { version = "1.0.16", default-features = false }
alloy-rpc-types = { version = "1.0.16", features = ["eth"], default-features = false }
alloy-rpc-types-admin = { version = "1.0.16", default-features = false }
alloy-rpc-types-anvil = { version = "1.0.16", default-features = false }
alloy-rpc-types-beacon = { version = "1.0.16", default-features = false }
alloy-rpc-types-debug = { version = "1.0.16", default-features = false }
alloy-rpc-types-engine = { version = "1.0.16", default-features = false }
alloy-rpc-types-eth = { version = "1.0.16", default-features = false }
alloy-rpc-types-mev = { version = "1.0.16", default-features = false }
alloy-rpc-types-trace = { version = "1.0.16", default-features = false }
alloy-rpc-types-txpool = { version = "1.0.16", default-features = false }
alloy-serde = { version = "1.0.16", default-features = false }
alloy-signer = { version = "1.0.16", default-features = false }
alloy-signer-local = { version = "1.0.16", default-features = false }
alloy-transport = { version = "1.0.16" }
alloy-transport-http = { version = "1.0.16", features = ["reqwest-rustls-tls"], default-features = false }
alloy-transport-ipc = { version = "1.0.16", default-features = false }
alloy-transport-ws = { version = "1.0.16", default-features = false }
# op
alloy-op-evm = { version = "0.12", default-features = false }

View File

@@ -650,21 +650,18 @@ impl SignerRecoverable for TransactionSigned {
let signature_hash = self.signature_hash();
recover_signer_unchecked(&self.signature, signature_hash)
}
fn recover_unchecked_with_buf(&self, buf: &mut Vec<u8>) -> Result<Address, RecoveryError> {
self.encode_for_signing(buf);
let signature_hash = keccak256(buf);
recover_signer_unchecked(&self.signature, signature_hash)
}
}
impl SignedTransaction for TransactionSigned {
fn tx_hash(&self) -> &TxHash {
self.hash.get_or_init(|| self.recalculate_hash())
}
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError> {
self.encode_for_signing(buf);
let signature_hash = keccak256(buf);
recover_signer_unchecked(&self.signature, signature_hash)
}
}
#[cfg(test)]

View File

@@ -127,17 +127,8 @@ impl SignerRecoverable for OpTransactionSigned {
let signature_hash = signature_hash(transaction);
recover_signer_unchecked(signature, signature_hash)
}
}
impl SignedTransaction for OpTransactionSigned {
fn tx_hash(&self) -> &TxHash {
self.hash.get_or_init(|| self.recalculate_hash())
}
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError> {
fn recover_unchecked_with_buf(&self, buf: &mut Vec<u8>) -> Result<Address, RecoveryError> {
match &self.transaction {
// Optimism's Deposit transaction does not have a signature. Directly return the
// `from` address.
@@ -149,6 +140,12 @@ impl SignedTransaction for OpTransactionSigned {
};
recover_signer_unchecked(&self.signature, keccak256(buf))
}
}
impl SignedTransaction for OpTransactionSigned {
fn tx_hash(&self) -> &TxHash {
self.hash.get_or_init(|| self.recalculate_hash())
}
fn recalculate_hash(&self) -> B256 {
keccak256(self.encoded_2718())

View File

@@ -149,6 +149,10 @@ where
fn recover_signer_unchecked(&self) -> Result<Address, RecoveryError> {
delegate!(self => tx.recover_signer_unchecked())
}
fn recover_unchecked_with_buf(&self, buf: &mut Vec<u8>) -> Result<Address, RecoveryError> {
delegate!(self => tx.recover_unchecked_with_buf(buf))
}
}
impl<B, T> SignedTransaction for Extended<B, T>
@@ -162,13 +166,6 @@ where
Self::Other(tx) => tx.tx_hash(),
}
}
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError> {
delegate!(self => tx.recover_signer_unchecked_with_buf(buf))
}
}
impl<B, T> Typed2718 for Extended<B, T>

View File

@@ -1,10 +1,7 @@
//! API of a signed transaction.
use crate::{
crypto::secp256k1::recover_signer_unchecked, InMemorySize, MaybeCompact, MaybeSerde,
MaybeSerdeBincodeCompat,
};
use alloc::{fmt, vec::Vec};
use crate::{InMemorySize, MaybeCompact, MaybeSerde, MaybeSerdeBincodeCompat};
use alloc::fmt;
use alloy_consensus::{
transaction::{Recovered, RlpEcdsaEncodableTx, SignerRecoverable},
EthereumTxEnvelope, SignableTransaction,
@@ -77,14 +74,6 @@ pub trait SignedTransaction:
self.recover_signer_unchecked()
}
/// Same as [`SignerRecoverable::recover_signer_unchecked`] but receives a buffer to operate on.
/// This is used during batch recovery to avoid allocating a new buffer for each
/// transaction.
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError>;
/// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with
/// tx type.
fn recalculate_hash(&self) -> B256 {
@@ -150,21 +139,6 @@ where
Self::Eip4844(tx) => tx.hash(),
}
}
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError> {
match self {
Self::Legacy(tx) => tx.tx().encode_for_signing(buf),
Self::Eip2930(tx) => tx.tx().encode_for_signing(buf),
Self::Eip1559(tx) => tx.tx().encode_for_signing(buf),
Self::Eip7702(tx) => tx.tx().encode_for_signing(buf),
Self::Eip4844(tx) => tx.tx().encode_for_signing(buf),
}
let signature_hash = keccak256(buf);
recover_signer_unchecked(self.signature(), signature_hash)
}
}
#[cfg(feature = "op")]
@@ -181,20 +155,6 @@ mod op {
Self::Eip7702(tx) => tx.hash(),
}
}
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError> {
match self {
Self::Legacy(tx) => tx.tx().encode_for_signing(buf),
Self::Eip2930(tx) => tx.tx().encode_for_signing(buf),
Self::Eip1559(tx) => tx.tx().encode_for_signing(buf),
Self::Eip7702(tx) => tx.tx().encode_for_signing(buf),
}
let signature_hash = keccak256(buf);
recover_signer_unchecked(self.signature(), signature_hash)
}
}
impl SignedTransaction for OpTxEnvelope {
@@ -207,27 +167,5 @@ mod op {
Self::Deposit(tx) => tx.hash_ref(),
}
}
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError> {
match self {
Self::Deposit(tx) => return Ok(tx.from),
Self::Legacy(tx) => tx.tx().encode_for_signing(buf),
Self::Eip2930(tx) => tx.tx().encode_for_signing(buf),
Self::Eip1559(tx) => tx.tx().encode_for_signing(buf),
Self::Eip7702(tx) => tx.tx().encode_for_signing(buf),
}
let signature_hash = keccak256(buf);
let signature = match self {
Self::Legacy(tx) => tx.signature(),
Self::Eip2930(tx) => tx.signature(),
Self::Eip1559(tx) => tx.signature(),
Self::Eip7702(tx) => tx.signature(),
Self::Deposit(_) => unreachable!("Deposit transactions should not be handled here"),
};
recover_signer_unchecked(signature, signature_hash)
}
}
}

View File

@@ -315,7 +315,7 @@ fn recover_sender<T: SignedTransaction>(
// value is greater than `secp256k1n / 2` if past EIP-2. There are transactions
// pre-homestead which have large `s` values, so using [Signature::recover_signer] here
// would not be backwards-compatible.
let sender = tx.recover_signer_unchecked_with_buf(rlp_buf).map_err(|_| {
let sender = tx.recover_unchecked_with_buf(rlp_buf).map_err(|_| {
SenderRecoveryStageError::FailedRecovery(FailedSenderRecoveryError { tx: tx_id })
})?;

View File

@@ -5,13 +5,13 @@ use alloy_consensus::{
RecoveryError,
},
transaction::SignerRecoverable,
SignableTransaction, Signed, Transaction, TransactionEnvelope,
Signed, Transaction, TransactionEnvelope,
};
use alloy_eips::{
eip2718::{Eip2718Result, IsTyped2718},
Decodable2718, Encodable2718, Typed2718,
};
use alloy_primitives::{bytes::Buf, keccak256, Sealed, Signature, TxHash, B256};
use alloy_primitives::{bytes::Buf, Sealed, Signature, TxHash, B256};
use alloy_rlp::{BufMut, Decodable, Encodable, Result as RlpResult};
use op_alloy_consensus::{OpTxEnvelope, TxDeposit};
use reth_codecs::{
@@ -128,15 +128,6 @@ impl SignedTransaction for CustomTransactionEnvelope {
fn tx_hash(&self) -> &TxHash {
self.inner.hash()
}
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError> {
self.inner.tx().encode_for_signing(buf);
let signature_hash = keccak256(buf);
recover_signer_unchecked(self.inner.signature(), signature_hash)
}
}
impl Typed2718 for CustomTransactionEnvelope {
@@ -300,20 +291,6 @@ impl SignerRecoverable for CustomTransaction {
}
impl SignedTransaction for CustomTransaction {
fn recover_signer_unchecked_with_buf(
&self,
buf: &mut Vec<u8>,
) -> Result<Address, RecoveryError> {
match self {
CustomTransaction::Op(tx) => {
SignedTransaction::recover_signer_unchecked_with_buf(tx, buf)
}
CustomTransaction::Payment(tx) => {
SignedTransaction::recover_signer_unchecked_with_buf(tx, buf)
}
}
}
fn tx_hash(&self) -> &B256 {
match self {
CustomTransaction::Op(tx) => SignedTransaction::tx_hash(tx),