1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use crate::{
    bytes::Bytes, transaction::AccessList, Action, SignedTransaction,
    Transaction, TransactionWithSignature,
    TransactionWithSignatureSerializePart,
};
use cfx_types::{AddressWithSpace, H256, U256};
use rlp::{Encodable, RlpStream};
use serde_derive::{Deserialize, Serialize};

impl Eip155Transaction {
    /// Fake sign phantom transactions.
    // The signature is part of the hash input. This implementation
    // ensures that phantom transactions whose fields are identical
    // will have different hashes.
    pub fn fake_sign_phantom(
        self, from: AddressWithSpace,
    ) -> SignedTransaction {
        SignedTransaction {
            transaction: TransactionWithSignature {
                transaction: TransactionWithSignatureSerializePart {
                    unsigned: Transaction::Ethereum(
                        EthereumTransaction::Eip155(self),
                    ),
                    // we use sender address for `r` and `s` so that
                    // phantom transactions with matching
                    // fields from different senders
                    // will have different hashes
                    r: U256::from(from.address.as_ref()),
                    s: U256::from(from.address.as_ref()),
                    v: 0,
                },
                hash: H256::zero(),
                rlp_size: None,
            }
            .compute_hash(),
            sender: from.address,
            public: None,
        }
    }

    /// Fake sign call requests in `eth_call`.
    // `fake_sign_phantom` will use zero signature when the sender is the
    // zero address, and that will fail basic signature verification.
    pub fn fake_sign_rpc(self, from: AddressWithSpace) -> SignedTransaction {
        SignedTransaction {
            transaction: TransactionWithSignature {
                transaction: TransactionWithSignatureSerializePart {
                    unsigned: Transaction::Ethereum(
                        EthereumTransaction::Eip155(self),
                    ),
                    r: U256::one(),
                    s: U256::one(),
                    v: 0,
                },
                hash: H256::zero(),
                rlp_size: None,
            }
            .compute_hash(),
            sender: from.address,
            public: None,
        }
    }
}

#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Eip155Transaction {
    /// Nonce.
    pub nonce: U256,
    /// Gas price.
    pub gas_price: U256,
    /// Gas paid up front for transaction execution.
    pub gas: U256,
    /// Action, can be either call or contract create.
    pub action: Action,
    /// Transferred value.
    pub value: U256,
    /// The chain id of the transaction
    pub chain_id: Option<u32>,
    /// Transaction data.
    pub data: Bytes,
}

impl Encodable for Eip155Transaction {
    fn rlp_append(&self, s: &mut RlpStream) {
        match self.chain_id {
            Some(chain_id) => {
                s.begin_list(9);
                s.append(&self.nonce);
                s.append(&self.gas_price);
                s.append(&self.gas);
                s.append(&self.action);
                s.append(&self.value);
                s.append(&self.data);
                s.append(&chain_id);
                s.append(&0u8);
                s.append(&0u8);
            }
            None => {
                s.begin_list(6);
                s.append(&self.nonce);
                s.append(&self.gas_price);
                s.append(&self.gas);
                s.append(&self.action);
                s.append(&self.value);
                s.append(&self.data);
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Eip2930Transaction {
    pub chain_id: u32,
    pub nonce: U256,
    pub gas_price: U256,
    pub gas: U256,
    pub action: Action,
    pub value: U256,
    pub data: Bytes,
    pub access_list: AccessList,
}

impl Encodable for Eip2930Transaction {
    fn rlp_append(&self, s: &mut RlpStream) {
        s.begin_list(8);
        s.append(&self.chain_id);
        s.append(&self.nonce);
        s.append(&self.gas_price);
        s.append(&self.gas);
        s.append(&self.action);
        s.append(&self.value);
        s.append(&self.data);
        s.append_list(&self.access_list);
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Eip1559Transaction {
    pub chain_id: u32,
    pub nonce: U256,
    pub max_priority_fee_per_gas: U256,
    pub max_fee_per_gas: U256,
    pub gas: U256,
    pub action: Action,
    pub value: U256,
    pub data: Bytes,
    pub access_list: AccessList,
}

impl Encodable for Eip1559Transaction {
    fn rlp_append(&self, s: &mut RlpStream) {
        s.begin_list(9);
        s.append(&self.chain_id);
        s.append(&self.nonce);
        s.append(&self.max_priority_fee_per_gas);
        s.append(&self.max_fee_per_gas);
        s.append(&self.gas);
        s.append(&self.action);
        s.append(&self.value);
        s.append(&self.data);
        s.append_list(&self.access_list);
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EthereumTransaction {
    Eip155(Eip155Transaction),
    Eip1559(Eip1559Transaction),
    Eip2930(Eip2930Transaction),
}
use EthereumTransaction::*;

impl EthereumTransaction {
    pub fn fake_sign_rpc(self, from: AddressWithSpace) -> SignedTransaction {
        SignedTransaction {
            transaction: TransactionWithSignature {
                transaction: TransactionWithSignatureSerializePart {
                    unsigned: Transaction::Ethereum(self),
                    r: U256::one(),
                    s: U256::one(),
                    v: 0,
                },
                hash: H256::zero(),
                rlp_size: None,
            }
            .compute_hash(),
            sender: from.address,
            public: None,
        }
    }
}

macro_rules! eth_access_common_ref {
    ($field:ident, $ty:ty) => {
        pub fn $field(&self) -> &$ty {
            match self {
                EthereumTransaction::Eip155(tx) => &tx.$field,
                EthereumTransaction::Eip2930(tx) => &tx.$field,
                EthereumTransaction::Eip1559(tx) => &tx.$field,
            }
        }
    };
}

impl EthereumTransaction {
    eth_access_common_ref!(gas, U256);

    eth_access_common_ref!(data, Bytes);

    eth_access_common_ref!(nonce, U256);

    eth_access_common_ref!(action, Action);

    eth_access_common_ref!(value, U256);

    pub fn gas_price(&self) -> &U256 {
        match self {
            Eip155(tx) => &tx.gas_price,
            Eip1559(tx) => &tx.max_fee_per_gas,
            Eip2930(tx) => &tx.gas_price,
        }
    }

    pub fn max_priority_gas_price(&self) -> &U256 {
        match self {
            Eip155(tx) => &tx.gas_price,
            Eip1559(tx) => &tx.max_priority_fee_per_gas,
            Eip2930(tx) => &tx.gas_price,
        }
    }

    pub fn chain_id(&self) -> Option<u32> {
        match self {
            Eip155(tx) => tx.chain_id,
            Eip1559(tx) => Some(tx.chain_id),
            Eip2930(tx) => Some(tx.chain_id),
        }
    }

    pub fn nonce_mut(&mut self) -> &mut U256 {
        match self {
            Eip155(tx) => &mut tx.nonce,
            Eip2930(tx) => &mut tx.nonce,
            Eip1559(tx) => &mut tx.nonce,
        }
    }

    pub fn access_list(&self) -> Option<&AccessList> {
        match self {
            Eip155(_tx) => None,
            Eip2930(tx) => Some(&tx.access_list),
            Eip1559(tx) => Some(&tx.access_list),
        }
    }
}

/// Replay protection logic for v part of transaction's signature
pub mod eip155_signature {
    /// Adds chain id into v
    pub fn add_chain_replay_protection(v: u8, chain_id: Option<u64>) -> u64 {
        v as u64
            + if let Some(n) = chain_id {
                35 + n * 2
            } else {
                27
            }
    }

    /// Returns refined v
    /// 0 if `v` would have been 27 under "Electrum" notation, 1 if 28 or 4 if
    /// invalid.
    pub fn extract_standard_v(v: u64) -> u8 {
        match v {
            v if v == 27 => 0,
            v if v == 28 => 1,
            v if v >= 35 => ((v - 1) % 2) as u8,
            _ => 4,
        }
    }

    pub fn extract_chain_id_from_legacy_v(v: u64) -> Option<u64> {
        if v >= 35 {
            Some((v - 35) / 2 as u64)
        } else {
            None
        }
    }
}