foundry_primitives/transaction/
envelope.rs

1use alloy_consensus::{
2    Sealed, Signed, TransactionEnvelope, TxEip1559, TxEip2930, TxEnvelope, TxLegacy, TxType,
3    Typed2718,
4    crypto::RecoveryError,
5    transaction::{
6        TxEip7702,
7        eip4844::{TxEip4844Variant, TxEip4844WithSidecar},
8    },
9};
10use alloy_evm::FromRecoveredTx;
11use alloy_network::{AnyRpcTransaction, AnyTxEnvelope};
12use alloy_primitives::{Address, B256};
13use alloy_rlp::Encodable;
14use alloy_rpc_types::ConversionError;
15use alloy_serde::WithOtherFields;
16use op_alloy_consensus::{DEPOSIT_TX_TYPE_ID, OpTransaction as OpTransactionTrait, TxDeposit};
17use op_revm::OpTransaction;
18use revm::context::TxEnv;
19use tempo_primitives::{AASigned, TempoTransaction};
20
21//
22/// Container type for signed, typed transactions.
23// NOTE(onbjerg): Boxing `Tempo(AASigned)` breaks `TransactionEnvelope` derive macro trait bounds.
24#[allow(clippy::large_enum_variant)]
25#[derive(Clone, Debug, TransactionEnvelope)]
26#[envelope(
27    tx_type_name = FoundryTxType,
28    typed = FoundryTypedTx,
29)]
30pub enum FoundryTxEnvelope {
31    /// Legacy transaction type
32    #[envelope(ty = 0)]
33    Legacy(Signed<TxLegacy>),
34    /// [EIP-2930] transaction.
35    ///
36    /// [EIP-2930]: https://eips.ethereum.org/EIPS/eip-2930
37    #[envelope(ty = 1)]
38    Eip2930(Signed<TxEip2930>),
39    /// [EIP-1559] transaction.
40    ///
41    /// [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
42    #[envelope(ty = 2)]
43    Eip1559(Signed<TxEip1559>),
44    /// [EIP-4844] transaction.
45    ///
46    /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
47    #[envelope(ty = 3)]
48    Eip4844(Signed<TxEip4844Variant>),
49    /// [EIP-7702] transaction.
50    ///
51    /// [EIP-7702]: https://eips.ethereum.org/EIPS/eip-7702
52    #[envelope(ty = 4)]
53    Eip7702(Signed<TxEip7702>),
54    /// OP stack deposit transaction.
55    ///
56    /// See <https://docs.optimism.io/op-stack/bridging/deposit-flow>.
57    #[envelope(ty = 126)]
58    Deposit(Sealed<TxDeposit>),
59    /// Tempo transaction type.
60    ///
61    /// See <https://docs.tempo.xyz/protocol/transactions>.
62    #[envelope(ty = 0x76, typed = TempoTransaction)]
63    Tempo(AASigned),
64}
65
66impl FoundryTxEnvelope {
67    /// Converts the transaction into an Ethereum [`TxEnvelope`].
68    ///
69    /// Returns an error if the transaction is not part of the standard Ethereum transaction types.
70    pub fn try_into_eth(self) -> Result<TxEnvelope, Self> {
71        match self {
72            Self::Legacy(tx) => Ok(TxEnvelope::Legacy(tx)),
73            Self::Eip2930(tx) => Ok(TxEnvelope::Eip2930(tx)),
74            Self::Eip1559(tx) => Ok(TxEnvelope::Eip1559(tx)),
75            Self::Eip4844(tx) => Ok(TxEnvelope::Eip4844(tx)),
76            Self::Eip7702(tx) => Ok(TxEnvelope::Eip7702(tx)),
77            Self::Deposit(_) => Err(self),
78            Self::Tempo(_) => Err(self),
79        }
80    }
81
82    pub fn sidecar(&self) -> Option<&TxEip4844WithSidecar> {
83        match self {
84            Self::Eip4844(signed_variant) => match signed_variant.tx() {
85                TxEip4844Variant::TxEip4844WithSidecar(with_sidecar) => Some(with_sidecar),
86                _ => None,
87            },
88            _ => None,
89        }
90    }
91
92    /// Returns the hash of the transaction.
93    ///
94    /// # Note
95    ///
96    /// If this transaction has the Impersonated signature then this returns a modified unique
97    /// hash. This allows us to treat impersonated transactions as unique.
98    pub fn hash(&self) -> B256 {
99        match self {
100            Self::Legacy(t) => *t.hash(),
101            Self::Eip2930(t) => *t.hash(),
102            Self::Eip1559(t) => *t.hash(),
103            Self::Eip4844(t) => *t.hash(),
104            Self::Eip7702(t) => *t.hash(),
105            Self::Deposit(t) => t.tx_hash(),
106            Self::Tempo(t) => *t.hash(),
107        }
108    }
109
110    /// Returns the hash if the transaction is impersonated (using a fake signature)
111    ///
112    /// This appends the `address` before hashing it
113    pub fn impersonated_hash(&self, sender: Address) -> B256 {
114        let mut buffer = Vec::new();
115        Encodable::encode(self, &mut buffer);
116        buffer.extend_from_slice(sender.as_ref());
117        B256::from_slice(alloy_primitives::utils::keccak256(&buffer).as_slice())
118    }
119
120    /// Recovers the Ethereum address which was used to sign the transaction.
121    pub fn recover(&self) -> Result<Address, RecoveryError> {
122        Ok(match self {
123            Self::Legacy(tx) => tx.recover_signer()?,
124            Self::Eip2930(tx) => tx.recover_signer()?,
125            Self::Eip1559(tx) => tx.recover_signer()?,
126            Self::Eip4844(tx) => tx.recover_signer()?,
127            Self::Eip7702(tx) => tx.recover_signer()?,
128            Self::Deposit(tx) => tx.from,
129            Self::Tempo(tx) => tx.signature().recover_signer(&tx.signature_hash())?,
130        })
131    }
132}
133
134impl OpTransactionTrait for FoundryTxEnvelope {
135    fn is_deposit(&self) -> bool {
136        matches!(self, Self::Deposit(_))
137    }
138
139    fn as_deposit(&self) -> Option<&Sealed<TxDeposit>> {
140        match self {
141            Self::Deposit(tx) => Some(tx),
142            _ => None,
143        }
144    }
145}
146
147impl TryFrom<FoundryTxEnvelope> for TxEnvelope {
148    type Error = FoundryTxEnvelope;
149
150    fn try_from(envelope: FoundryTxEnvelope) -> Result<Self, Self::Error> {
151        envelope.try_into_eth()
152    }
153}
154
155impl TryFrom<AnyRpcTransaction> for FoundryTxEnvelope {
156    type Error = ConversionError;
157
158    fn try_from(value: AnyRpcTransaction) -> Result<Self, Self::Error> {
159        let WithOtherFields { inner, .. } = value.0;
160        let from = inner.inner.signer();
161        match inner.inner.into_inner() {
162            AnyTxEnvelope::Ethereum(tx) => match tx {
163                TxEnvelope::Legacy(tx) => Ok(Self::Legacy(tx)),
164                TxEnvelope::Eip2930(tx) => Ok(Self::Eip2930(tx)),
165                TxEnvelope::Eip1559(tx) => Ok(Self::Eip1559(tx)),
166                TxEnvelope::Eip4844(tx) => Ok(Self::Eip4844(tx)),
167                TxEnvelope::Eip7702(tx) => Ok(Self::Eip7702(tx)),
168            },
169            AnyTxEnvelope::Unknown(mut tx) => {
170                // Try to convert to deposit transaction
171                if tx.ty() == DEPOSIT_TX_TYPE_ID {
172                    tx.inner.fields.insert("from".to_string(), serde_json::to_value(from).unwrap());
173                    let deposit_tx =
174                        tx.inner.fields.deserialize_into::<TxDeposit>().map_err(|e| {
175                            ConversionError::Custom(format!(
176                                "Failed to deserialize deposit tx: {e}"
177                            ))
178                        })?;
179
180                    return Ok(Self::Deposit(Sealed::new(deposit_tx)));
181                };
182
183                let tx_type = tx.ty();
184                Err(ConversionError::Custom(format!("Unknown transaction type: 0x{tx_type:02X}")))
185            }
186        }
187    }
188}
189
190impl FromRecoveredTx<FoundryTxEnvelope> for TxEnv {
191    fn from_recovered_tx(tx: &FoundryTxEnvelope, caller: Address) -> Self {
192        match tx {
193            FoundryTxEnvelope::Legacy(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
194            FoundryTxEnvelope::Eip2930(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
195            FoundryTxEnvelope::Eip1559(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
196            FoundryTxEnvelope::Eip4844(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
197            FoundryTxEnvelope::Eip7702(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
198            FoundryTxEnvelope::Deposit(sealed_tx) => {
199                Self::from_recovered_tx(sealed_tx.inner(), caller)
200            }
201            FoundryTxEnvelope::Tempo(_) => panic!("unsupported tx type on ethereum"),
202        }
203    }
204}
205
206impl FromRecoveredTx<FoundryTxEnvelope> for OpTransaction<TxEnv> {
207    fn from_recovered_tx(tx: &FoundryTxEnvelope, caller: Address) -> Self {
208        match tx {
209            FoundryTxEnvelope::Legacy(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
210            FoundryTxEnvelope::Eip2930(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
211            FoundryTxEnvelope::Eip1559(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
212            FoundryTxEnvelope::Eip4844(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
213            FoundryTxEnvelope::Eip7702(signed_tx) => Self::from_recovered_tx(signed_tx, caller),
214            FoundryTxEnvelope::Deposit(sealed_tx) => {
215                Self::from_recovered_tx(sealed_tx.inner(), caller)
216            }
217            FoundryTxEnvelope::Tempo(_) => panic!("unsupported tx type on optimism"),
218        }
219    }
220}
221
222impl std::fmt::Display for FoundryTxType {
223    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
224        match self {
225            Self::Legacy => write!(f, "legacy"),
226            Self::Eip2930 => write!(f, "eip2930"),
227            Self::Eip1559 => write!(f, "eip1559"),
228            Self::Eip4844 => write!(f, "eip4844"),
229            Self::Eip7702 => write!(f, "eip7702"),
230            Self::Deposit => write!(f, "deposit"),
231            Self::Tempo => write!(f, "tempo"),
232        }
233    }
234}
235
236impl From<TxType> for FoundryTxType {
237    fn from(tx: TxType) -> Self {
238        match tx {
239            TxType::Legacy => Self::Legacy,
240            TxType::Eip2930 => Self::Eip2930,
241            TxType::Eip1559 => Self::Eip1559,
242            TxType::Eip4844 => Self::Eip4844,
243            TxType::Eip7702 => Self::Eip7702,
244        }
245    }
246}
247
248impl From<FoundryTxEnvelope> for FoundryTypedTx {
249    fn from(envelope: FoundryTxEnvelope) -> Self {
250        match envelope {
251            FoundryTxEnvelope::Legacy(signed_tx) => Self::Legacy(signed_tx.strip_signature()),
252            FoundryTxEnvelope::Eip2930(signed_tx) => Self::Eip2930(signed_tx.strip_signature()),
253            FoundryTxEnvelope::Eip1559(signed_tx) => Self::Eip1559(signed_tx.strip_signature()),
254            FoundryTxEnvelope::Eip4844(signed_tx) => Self::Eip4844(signed_tx.strip_signature()),
255            FoundryTxEnvelope::Eip7702(signed_tx) => Self::Eip7702(signed_tx.strip_signature()),
256            FoundryTxEnvelope::Deposit(sealed_tx) => Self::Deposit(sealed_tx.into_inner()),
257            FoundryTxEnvelope::Tempo(signed_tx) => Self::Tempo(signed_tx.strip_signature()),
258        }
259    }
260}
261
262#[cfg(test)]
263mod tests {
264    use std::str::FromStr;
265
266    use alloy_primitives::{Bytes, Signature, TxHash, TxKind, U256, b256, hex};
267    use alloy_rlp::Decodable;
268
269    use super::*;
270
271    #[test]
272    fn test_decode_call() {
273        let bytes_first = &mut &hex::decode("f86b02843b9aca00830186a094d3e8763675e4c425df46cc3b5c0f6cbdac39604687038d7ea4c68000802ba00eb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5aea03a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca18").unwrap()[..];
274        let decoded = FoundryTxEnvelope::decode(&mut &bytes_first[..]).unwrap();
275
276        let tx = TxLegacy {
277            nonce: 2u64,
278            gas_price: 1000000000u128,
279            gas_limit: 100000,
280            to: TxKind::Call(Address::from_slice(
281                &hex::decode("d3e8763675e4c425df46cc3b5c0f6cbdac396046").unwrap()[..],
282            )),
283            value: U256::from(1000000000000000u64),
284            input: Bytes::default(),
285            chain_id: Some(4),
286        };
287
288        let signature = Signature::from_str("0eb96ca19e8a77102767a41fc85a36afd5c61ccb09911cec5d3e86e193d9c5ae3a456401896b1b6055311536bf00a718568c744d8c1f9df59879e8350220ca182b").unwrap();
289
290        let tx = FoundryTxEnvelope::Legacy(Signed::new_unchecked(
291            tx,
292            signature,
293            b256!("0xa517b206d2223278f860ea017d3626cacad4f52ff51030dc9a96b432f17f8d34"),
294        ));
295
296        assert_eq!(tx, decoded);
297    }
298
299    #[test]
300    fn test_decode_create_goerli() {
301        // test that an example create tx from goerli decodes properly
302        let tx_bytes =
303              hex::decode("02f901ee05228459682f008459682f11830209bf8080b90195608060405234801561001057600080fd5b50610175806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c80630c49c36c14610030575b600080fd5b61003861004e565b604051610045919061011d565b60405180910390f35b60606020600052600f6020527f68656c6c6f2073746174656d696e64000000000000000000000000000000000060405260406000f35b600081519050919050565b600082825260208201905092915050565b60005b838110156100be5780820151818401526020810190506100a3565b838111156100cd576000848401525b50505050565b6000601f19601f8301169050919050565b60006100ef82610084565b6100f9818561008f565b93506101098185602086016100a0565b610112816100d3565b840191505092915050565b6000602082019050818103600083015261013781846100e4565b90509291505056fea264697066735822122051449585839a4ea5ac23cae4552ef8a96b64ff59d0668f76bfac3796b2bdbb3664736f6c63430008090033c080a0136ebffaa8fc8b9fda9124de9ccb0b1f64e90fbd44251b4c4ac2501e60b104f9a07eb2999eec6d185ef57e91ed099afb0a926c5b536f0155dd67e537c7476e1471")
304                  .unwrap();
305        let _decoded = FoundryTxEnvelope::decode(&mut &tx_bytes[..]).unwrap();
306    }
307
308    #[test]
309    fn can_recover_sender() {
310        // random mainnet tx: https://etherscan.io/tx/0x86718885c4b4218c6af87d3d0b0d83e3cc465df2a05c048aa4db9f1a6f9de91f
311        let bytes = hex::decode("02f872018307910d808507204d2cb1827d0094388c818ca8b9251b393131c08a736a67ccb19297880320d04823e2701c80c001a0cf024f4815304df2867a1a74e9d2707b6abda0337d2d54a4438d453f4160f190a07ac0e6b3bc9395b5b9c8b9e6d77204a236577a5b18467b9175c01de4faa208d9").unwrap();
312
313        let Ok(FoundryTxEnvelope::Eip1559(tx)) = FoundryTxEnvelope::decode(&mut &bytes[..]) else {
314            panic!("decoding FoundryTxEnvelope failed");
315        };
316
317        assert_eq!(
318            tx.hash(),
319            &"0x86718885c4b4218c6af87d3d0b0d83e3cc465df2a05c048aa4db9f1a6f9de91f"
320                .parse::<B256>()
321                .unwrap()
322        );
323        assert_eq!(
324            tx.recover_signer().unwrap(),
325            "0x95222290DD7278Aa3Ddd389Cc1E1d165CC4BAfe5".parse::<Address>().unwrap()
326        );
327    }
328
329    // Test vector from https://sepolia.etherscan.io/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0
330    // Blobscan: https://sepolia.blobscan.com/tx/0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0
331    #[test]
332    fn test_decode_live_4844_tx() {
333        use alloy_primitives::{address, b256};
334
335        // https://sepolia.etherscan.io/getRawTx?tx=0x9a22ccb0029bc8b0ddd073be1a1d923b7ae2b2ea52100bae0db4424f9107e9c0
336        let raw_tx = alloy_primitives::hex::decode("0x03f9011d83aa36a7820fa28477359400852e90edd0008252089411e9ca82a3a762b4b5bd264d4173a242e7a770648080c08504a817c800f8a5a0012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921aa00152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4a0013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7a001148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1a0011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e654901a0c8de4cced43169f9aa3d36506363b2d2c44f6c49fc1fd91ea114c86f3757077ea01e11fdd0d1934eda0492606ee0bb80a7bf8f35cc5f86ec60fe5031ba48bfd544").unwrap();
337        let res = FoundryTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap();
338        assert!(res.is_type(3));
339
340        let tx = match res {
341            FoundryTxEnvelope::Eip4844(tx) => tx,
342            _ => unreachable!(),
343        };
344
345        assert_eq!(tx.tx().tx().to, address!("0x11E9CA82A3a762b4B5bd264d4173a242e7a77064"));
346
347        assert_eq!(
348            tx.tx().tx().blob_versioned_hashes,
349            vec![
350                b256!("0x012ec3d6f66766bedb002a190126b3549fce0047de0d4c25cffce0dc1c57921a"),
351                b256!("0x0152d8e24762ff22b1cfd9f8c0683786a7ca63ba49973818b3d1e9512cd2cec4"),
352                b256!("0x013b98c6c83e066d5b14af2b85199e3d4fc7d1e778dd53130d180f5077e2d1c7"),
353                b256!("0x01148b495d6e859114e670ca54fb6e2657f0cbae5b08063605093a4b3dc9f8f1"),
354                b256!("0x011ac212f13c5dff2b2c6b600a79635103d6f580a4221079951181b25c7e6549")
355            ]
356        );
357
358        let from = tx.recover_signer().unwrap();
359        assert_eq!(from, address!("0xA83C816D4f9b2783761a22BA6FADB0eB0606D7B2"));
360    }
361
362    #[test]
363    fn test_decode_encode_deposit_tx() {
364        // https://sepolia-optimism.etherscan.io/tx/0xbf8b5f08c43e4b860715cd64fc0849bbce0d0ea20a76b269e7bc8886d112fca7
365        let tx_hash: TxHash = "0xbf8b5f08c43e4b860715cd64fc0849bbce0d0ea20a76b269e7bc8886d112fca7"
366            .parse::<TxHash>()
367            .unwrap();
368
369        // https://sepolia-optimism.etherscan.io/getRawTx?tx=0xbf8b5f08c43e4b860715cd64fc0849bbce0d0ea20a76b269e7bc8886d112fca7
370        let raw_tx = alloy_primitives::hex::decode(
371            "7ef861a0dfd7ae78bf3c414cfaa77f13c0205c82eb9365e217b2daa3448c3156b69b27ac94778f2146f48179643473b82931c4cd7b8f153efd94778f2146f48179643473b82931c4cd7b8f153efd872386f26fc10000872386f26fc10000830186a08080",
372        )
373        .unwrap();
374        let dep_tx = FoundryTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap();
375
376        let mut encoded = Vec::new();
377        dep_tx.encode_2718(&mut encoded);
378
379        assert_eq!(raw_tx, encoded);
380
381        assert_eq!(tx_hash, dep_tx.hash());
382    }
383
384    #[test]
385    fn can_recover_sender_not_normalized() {
386        let bytes = hex::decode("f85f800182520894095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba048b55bfa915ac795c431978d8a6a992b628d557da5ff759b307d495a36649353a0efffd310ac743f371de3b9f7f9cb56c0b28ad43601b4ab949f53faa07bd2c804").unwrap();
387
388        let Ok(FoundryTxEnvelope::Legacy(tx)) = FoundryTxEnvelope::decode(&mut &bytes[..]) else {
389            panic!("decoding FoundryTxEnvelope failed");
390        };
391
392        assert_eq!(tx.tx().input, Bytes::from(b""));
393        assert_eq!(tx.tx().gas_price, 1);
394        assert_eq!(tx.tx().gas_limit, 21000);
395        assert_eq!(tx.tx().nonce, 0);
396        if let TxKind::Call(to) = tx.tx().to {
397            assert_eq!(
398                to,
399                "0x095e7baea6a6c7c4c2dfeb977efac326af552d87".parse::<Address>().unwrap()
400            );
401        } else {
402            panic!("expected a call transaction");
403        }
404        assert_eq!(tx.tx().value, U256::from(0x0au64));
405        assert_eq!(
406            tx.recover_signer().unwrap(),
407            "0f65fe9276bc9a24ae7083ae28e2660ef72df99e".parse::<Address>().unwrap()
408        );
409    }
410
411    #[test]
412    fn deser_to_type_tx() {
413        let tx = r#"
414        {
415            "type": "0x2",
416            "chainId": "0x7a69",
417            "nonce": "0x0",
418            "gas": "0x5209",
419            "maxFeePerGas": "0x77359401",
420            "maxPriorityFeePerGas": "0x1",
421            "to": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
422            "value": "0x0",
423            "accessList": [],
424            "input": "0x",
425            "r": "0x85c2794a580da137e24ccc823b45ae5cea99371ae23ee13860fcc6935f8305b0",
426            "s": "0x41de7fa4121dab284af4453d30928241208bafa90cdb701fe9bc7054759fe3cd",
427            "yParity": "0x0",
428            "hash": "0x8c9b68e8947ace33028dba167354fde369ed7bbe34911b772d09b3c64b861515"
429        }"#;
430
431        let _typed_tx: FoundryTxEnvelope = serde_json::from_str(tx).unwrap();
432    }
433
434    #[test]
435    fn test_from_recovered_tx_legacy() {
436        let tx = r#"
437        {
438            "type": "0x0",
439            "chainId": "0x1",
440            "nonce": "0x0",
441            "gas": "0x5208",
442            "gasPrice": "0x1",
443            "to": "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266",
444            "value": "0x1",
445            "input": "0x",
446            "r": "0x85c2794a580da137e24ccc823b45ae5cea99371ae23ee13860fcc6935f8305b0",
447            "s": "0x41de7fa4121dab284af4453d30928241208bafa90cdb701fe9bc7054759fe3cd",
448            "v": "0x1b",
449            "hash": "0x8c9b68e8947ace33028dba167354fde369ed7bbe34911b772d09b3c64b861515"
450        }"#;
451
452        let typed_tx: FoundryTxEnvelope = serde_json::from_str(tx).unwrap();
453        let sender = typed_tx.recover().unwrap();
454
455        // Test TxEnv conversion via FromRecoveredTx trait
456        let tx_env = TxEnv::from_recovered_tx(&typed_tx, sender);
457        assert_eq!(tx_env.caller, sender);
458        assert_eq!(tx_env.gas_limit, 0x5208);
459        assert_eq!(tx_env.gas_price, 1);
460
461        // Test OpTransaction<TxEnv> conversion via FromRecoveredTx trait
462        let op_tx = OpTransaction::<TxEnv>::from_recovered_tx(&typed_tx, sender);
463        assert_eq!(op_tx.base.caller, sender);
464        assert_eq!(op_tx.base.gas_limit, 0x5208);
465    }
466
467    // Test vector from Tempo testnet:
468    // https://explorer.testnet.tempo.xyz/tx/0x6d6d8c102064e6dee44abad2024a8b1d37959230baab80e70efbf9b0c739c4fd
469    #[test]
470    fn test_decode_encode_tempo_tx() {
471        use alloy_primitives::address;
472        use tempo_primitives::TEMPO_TX_TYPE_ID;
473
474        let tx_hash: TxHash = "0x6d6d8c102064e6dee44abad2024a8b1d37959230baab80e70efbf9b0c739c4fd"
475            .parse::<TxHash>()
476            .unwrap();
477
478        // Raw transaction from Tempo testnet via eth_getRawTransactionByHash
479        let raw_tx = hex::decode(
480            "76f9025e82a5bd808502cb4178008302d178f8fcf85c9420c000000000000000000000000000000000000080b844095ea7b3000000000000000000000000dec00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000989680f89c94dec000000000000000000000000000000000000080b884f8856c0f00000000000000000000000020c000000000000000000000000000000000000000000000000000000000000020c00000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000989680000000000000000000000000000000000000000000000000000000000097d330c0808080809420c000000000000000000000000000000000000180c0b90133027b98b7a8e6c68d7eac741a52e6fdae0560ce3c16ef5427ad46d7a54d0ed86dd41d000000007b2274797065223a22776562617574686e2e676574222c226368616c6c656e6765223a2238453071464a7a50585167546e645473643649456659457776323173516e626966374c4741776e4b43626b222c226f726967696e223a2268747470733a2f2f74656d706f2d6465782e76657263656c2e617070222c2263726f73734f726967696e223a66616c73657dcfd45c3b19745a42f80b134dcb02a8ba099a0e4e7be1984da54734aa81d8f29f74bb9170ae6d25bd510c83fe35895ee5712efe13980a5edc8094c534e23af85eaacc80b21e45fb11f349424dce3a2f23547f60c0ff2f8bcaede2a247545ce8dd87abf0dbb7a5c9507efae2e43833356651b45ac576c2e61cec4e9c0f41fcbf6e",
481        )
482        .unwrap();
483
484        let tempo_tx = FoundryTxEnvelope::decode(&mut raw_tx.as_slice()).unwrap();
485
486        // Verify it's a Tempo transaction (type 0x76)
487        assert!(tempo_tx.is_type(TEMPO_TX_TYPE_ID));
488
489        let FoundryTxEnvelope::Tempo(ref aa_signed) = tempo_tx else {
490            panic!("Expected Tempo transaction");
491        };
492
493        // Verify the chain ID
494        assert_eq!(aa_signed.tx().chain_id, 42429);
495
496        // Verify the fee token
497        assert_eq!(
498            aa_signed.tx().fee_token,
499            Some(address!("0x20C0000000000000000000000000000000000001"))
500        );
501
502        // Verify gas limit
503        assert_eq!(aa_signed.tx().gas_limit, 184696);
504
505        // Verify we have 2 calls
506        assert_eq!(aa_signed.tx().calls.len(), 2);
507
508        // Verify the hash
509        assert_eq!(tx_hash, tempo_tx.hash());
510
511        // Verify round-trip encoding
512        let mut encoded = Vec::new();
513        tempo_tx.encode_2718(&mut encoded);
514        assert_eq!(raw_tx, encoded);
515
516        // Verify sender recovery (WebAuthn signature)
517        let sender = tempo_tx.recover().unwrap();
518        assert_eq!(sender, address!("0x566Ff0f4a6114F8072ecDC8A7A8A13d8d0C6B45F"));
519    }
520}