Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add alloy-compat for signature and transaction #8197

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 64 additions & 3 deletions crates/primitives/src/alloy_compat.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Common conversions from alloy types.

use crate::{
Block, Header, Transaction, TransactionSigned, TxEip1559, TxEip2930, TxEip4844, TxLegacy,
TxType,
transaction::extract_chain_id, Block, Header, Signature, Transaction, TransactionSigned,
TransactionSignedEcRecovered, TxEip1559, TxEip2930, TxEip4844, TxLegacy, TxType,
};
use alloy_primitives::TxKind;
use alloy_rlp::Error as RlpError;
Expand Down Expand Up @@ -115,7 +115,7 @@ impl TryFrom<alloy_rpc_types::Transaction> for Transaction {
return Err(ConversionError::Eip2718Error(
RlpError::Custom("EIP-1559 fields are present in a legacy transaction")
.into(),
))
));
}
Ok(Transaction::Legacy(TxLegacy {
chain_id: tx.chain_id,
Expand Down Expand Up @@ -199,3 +199,64 @@ impl TryFrom<alloy_rpc_types::Transaction> for Transaction {
}
}
}

impl TryFrom<alloy_rpc_types::Transaction> for TransactionSigned {
type Error = alloy_rpc_types::ConversionError;

fn try_from(tx: alloy_rpc_types::Transaction) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;

let signature = tx.signature.ok_or(ConversionError::MissingSignature)?;
let transaction: Transaction = tx.try_into()?;

Ok(TransactionSigned::from_transaction_and_signature(
transaction.clone(),
Signature {
r: signature.r,
s: signature.s,
odd_y_parity: if let Some(y_parity) = signature.y_parity {
y_parity.0
} else {
match transaction.tx_type() {
// If the transaction type is Legacy, adjust the v component of the
// signature according to the Ethereum specification
TxType::Legacy => {
extract_chain_id(signature.v.to())
.map_err(|_| ConversionError::InvalidSignature)?
.0
}
_ => !signature.v.is_zero(),
}
},
},
))
}
}

impl TryFrom<alloy_rpc_types::Transaction> for TransactionSignedEcRecovered {
type Error = alloy_rpc_types::ConversionError;

fn try_from(tx: alloy_rpc_types::Transaction) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;

let transaction: TransactionSigned = tx.try_into()?;

transaction.try_into_ecrecovered().map_err(|_| ConversionError::InvalidSignature)
}
}

impl TryFrom<alloy_rpc_types::Signature> for Signature {
type Error = alloy_rpc_types::ConversionError;

fn try_from(signature: alloy_rpc_types::Signature) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;

let odd_y_parity = if let Some(y_parity) = signature.y_parity {
y_parity.0
} else {
extract_chain_id(signature.v.to()).map_err(|_| ConversionError::InvalidSignature)?.0
};

Ok(Self { r: signature.r, s: signature.s, odd_y_parity })
}
}
36 changes: 0 additions & 36 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1615,42 +1615,6 @@ impl IntoRecoveredTransaction for TransactionSignedEcRecovered {
}
}

#[cfg(feature = "alloy-compat")]
impl TryFrom<alloy_rpc_types::Transaction> for TransactionSignedEcRecovered {
type Error = alloy_rpc_types::ConversionError;

fn try_from(tx: alloy_rpc_types::Transaction) -> Result<Self, Self::Error> {
use alloy_rpc_types::ConversionError;
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?;

let transaction: Transaction = tx.try_into()?;

TransactionSigned::from_transaction_and_signature(
transaction.clone(),
Signature {
r: signature.r,
s: signature.s,
odd_y_parity: if let Some(y_parity) = signature.y_parity {
y_parity.0
} else {
match transaction.tx_type() {
// If the transaction type is Legacy, adjust the v component of the
// signature according to the Ethereum specification
TxType::Legacy => {
extract_chain_id(signature.v.to())
.map_err(|_| ConversionError::InvalidSignature)?
.0
}
_ => !signature.v.is_zero(),
}
},
},
)
.try_into_ecrecovered()
.map_err(|_| ConversionError::InvalidSignature)
}
}

#[cfg(test)]
mod tests {
use crate::{
Expand Down
Loading