diff --git a/src/eth/primitives/external_transaction.rs b/src/eth/primitives/external_transaction.rs index 4074028ce..6676d50d4 100644 --- a/src/eth/primitives/external_transaction.rs +++ b/src/eth/primitives/external_transaction.rs @@ -3,7 +3,6 @@ use ethers_core::types::Transaction as EthersTransaction; use crate::eth::evm::EvmExecutionResult; use crate::eth::primitives::ExternalReceipt; use crate::eth::primitives::Hash; -use crate::eth::primitives::TransactionInput; #[derive(Debug, derive_new::new)] pub struct ExternalTransactionExecution { @@ -23,13 +22,9 @@ impl ExternalTransaction { } } -impl TryFrom for TransactionInput { - type Error = anyhow::Error; - fn try_from(value: ExternalTransaction) -> Result { - value.0.try_into() - } -} - +// ----------------------------------------------------------------------------- +// Conversions: Other -> Self +// ----------------------------------------------------------------------------- impl From for ExternalTransaction { fn from(value: EthersTransaction) -> Self { ExternalTransaction(value) diff --git a/src/eth/primitives/transaction_input.rs b/src/eth/primitives/transaction_input.rs index 31d98d404..d3dd13825 100644 --- a/src/eth/primitives/transaction_input.rs +++ b/src/eth/primitives/transaction_input.rs @@ -18,6 +18,7 @@ use rlp::Decodable; use crate::eth::primitives::Address; use crate::eth::primitives::Bytes; use crate::eth::primitives::ChainId; +use crate::eth::primitives::ExternalTransaction; use crate::eth::primitives::Gas; use crate::eth::primitives::Hash; use crate::eth::primitives::Nonce; @@ -88,47 +89,61 @@ impl Decodable for TransactionInput { } // ----------------------------------------------------------------------------- -// Conversions: Other -> Self +// Conversion: Other -> Self // ----------------------------------------------------------------------------- +impl TryFrom for TransactionInput { + type Error = anyhow::Error; + + fn try_from(value: ExternalTransaction) -> anyhow::Result { + try_from_ethers_transaction(value.0, false) + } +} impl TryFrom for TransactionInput { type Error = anyhow::Error; fn try_from(value: EthersTransaction) -> anyhow::Result { - // extract signer - let signer: Address = match value.recover_from() { + try_from_ethers_transaction(value, true) + } +} + +fn try_from_ethers_transaction(value: EthersTransaction, compute_signer: bool) -> anyhow::Result { + // extract signer + let signer: Address = match compute_signer { + true => match value.recover_from() { Ok(signer) => signer.into(), Err(e) => { tracing::warn!(reason = ?e, "failed to recover transaction signer"); return Err(anyhow!("Transaction signer cannot be recovered. Check the transaction signature is valid.")); } - }; - - // extract gas price - let gas_price: Wei = match value.gas_price { - Some(wei) => wei.into(), - None => return log_and_err!("transaction without gas_price id is not allowed"), - }; - - Ok(Self { - chain_id: match value.chain_id { - Some(chain_id) => Some(chain_id.try_into()?), - None => None, - }, - hash: value.hash.into(), - nonce: value.nonce.try_into()?, - signer, - from: Address::new(value.from.into()), - to: value.to.map_into(), - value: value.value.into(), - input: value.input.clone().into(), - gas_limit: value.gas.try_into()?, - gas_price, - v: value.v, - r: value.r, - s: value.s, - }) - } + }, + false => value.from.into(), + }; + + // extract gas price + let gas_price: Wei = match value.gas_price { + Some(wei) => wei.into(), + None => return log_and_err!("transaction without gas_price id is not allowed"), + }; + + Ok(TransactionInput { + chain_id: match value.chain_id { + Some(chain_id) => Some(chain_id.try_into()?), + None => None, + }, + hash: value.hash.into(), + nonce: value.nonce.try_into()?, + signer, + from: Address::new(value.from.into()), + to: value.to.map_into(), + value: value.value.into(), + input: value.input.clone().into(), + gas_limit: value.gas.try_into()?, + gas_price, + v: value.v, + r: value.r, + s: value.s, + }) } // -----------------------------------------------------------------------------