pub fn rlp(
        &self,
        rlp: &mut RlpStream,
        signature: Option<&PackedEthSignature>,
    ) -> Result<(), SerializationTransactionError> {
        rlp.begin_unbounded_list();

        match self.transaction_type {
            // EIP-2930 (0x01)
            Some(x) if x == EIP_2930_TX_TYPE.into() => {
                rlp.append(
                    &self
                        .chain_id
                        .ok_or(SerializationTransactionError::WrongChainId(None))?,
                );
                rlp.append(&self.nonce);
                rlp.append(&self.gas_price);
                rlp.append(&self.gas);
                rlp_opt(rlp, &self.to);
                rlp.append(&self.value);
                rlp.append(&self.input.0);
                access_list_rlp(rlp, &self.access_list);
            }
            // EIP-1559 (0x02)
            Some(x) if x == EIP_1559_TX_TYPE.into() => {
                rlp.append(
                    &self
                        .chain_id
                        .ok_or(SerializationTransactionError::WrongChainId(None))?,
                );
                rlp.append(&self.nonce);
                rlp_opt(rlp, &self.max_priority_fee_per_gas);
                rlp.append(&self.gas_price);
                rlp.append(&self.gas);
                rlp_opt(rlp, &self.to);
                rlp.append(&self.value);
                rlp.append(&self.input.0);
                access_list_rlp(rlp, &self.access_list);
            }
            // EIP-712
            Some(x) if x == EIP_712_TX_TYPE.into() => {
                rlp.append(&self.nonce);
                rlp_opt(rlp, &self.max_priority_fee_per_gas);
                rlp.append(&self.gas_price);
                rlp.append(&self.gas);
                rlp_opt(rlp, &self.to);
                rlp.append(&self.value);
                rlp.append(&self.input.0);
            }
            Some(x) if x == LEGACY_TX_TYPE.into() => {
                rlp.append(&self.nonce);
                rlp.append(&self.gas_price);
                rlp.append(&self.gas);
                rlp_opt(rlp, &self.to);
                rlp.append(&self.value);
                rlp.append(&self.input.0);
            }
            // Legacy (None)
            None => {
                rlp.append(&self.nonce);
                rlp.append(&self.gas_price);
                rlp.append(&self.gas);
                rlp_opt(rlp, &self.to);
                rlp.append(&self.value);
                rlp.append(&self.input.0);
            }
            Some(_) => unreachable!("Unknown tx type"),
        }

        match (signature, self.chain_id, self.is_legacy_tx()) {
            (Some(sig), Some(chain_id), true) => {
                rlp.append(&sig.v_with_chain_id(chain_id));
                rlp.append(&U256::from_big_endian(sig.r()));
                rlp.append(&U256::from_big_endian(sig.s()));
            }
            (None, Some(chain_id), true) => {
                rlp.append(&chain_id);
                rlp.append(&0u8);
                rlp.append(&0u8);
            }
            (Some(sig), _, _) => {
                rlp.append(&sig.v());
                rlp.append(&U256::from_big_endian(sig.r()));
                rlp.append(&U256::from_big_endian(sig.s()));
            }
            (None, _, _) => {}
        }

        if self.is_eip712_tx() {
            rlp.append(
                &self
                    .chain_id
                    .ok_or(SerializationTransactionError::WrongChainId(None))?,
            );
            rlp_opt(rlp, &self.from);
            if let Some(meta) = &self.eip712_meta {
                meta.rlp_append(rlp);
            }
        }

        rlp.finalize_unbounded_list();
        Ok(())
    }
Encodes `TransactionRequest` to RLP.
It may fail if `chain_id` is `None` while required.
core/lib/types/src/transaction_request.rs