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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use crate::address::EthereumAddress;
use crate::amount::EthereumAmount;
use crate::format::EthereumFormat;
use crate::network::EthereumNetwork;
use crate::public_key::EthereumPublicKey;
use anychain_core::ethereum_types::U256;
use anychain_core::utilities::crypto::keccak256;
use anychain_core::{hex, libsecp256k1, PublicKey, Transaction, TransactionError, TransactionId};
use core::{fmt, marker::PhantomData, str::FromStr};
use ethabi::ethereum_types::H160;
use ethabi::{Function, Param, ParamType, StateMutability, Token};
use rlp::{decode_list, RlpStream};
use std::convert::TryInto;

/// Trim the leading zeros of a byte stream and return it
fn trim_leading_zeros(v: &Vec<u8>) -> &[u8] {
    let mut cnt: usize = 0;
    for byte in v {
        if *byte != 0 {
            break;
        } else {
            cnt += 1;
        }
    }
    &v[cnt..]
}

/// Prepend a number of zeros to 'v' to make it 'to_len' bytes long
fn pad_zeros(v: &mut Vec<u8>, to_len: usize) {
    if v.len() < to_len {
        let mut temp = v.clone();
        let len = v.len();
        v.clear();
        v.resize(to_len - len, 0);
        v.append(&mut temp);
    }
}

pub fn encode_transfer(func_name: &str, address: &EthereumAddress, amount: U256) -> Vec<u8> {
    #[allow(deprecated)]
    let func = Function {
        name: func_name.to_string(),
        inputs: vec![
            Param {
                name: "address".to_string(),
                kind: ParamType::Address,
                internal_type: None,
            },
            Param {
                name: "amount".to_string(),
                kind: ParamType::Uint(256),
                internal_type: None,
            },
        ],
        outputs: vec![],
        constant: None,
        state_mutability: StateMutability::Payable,
    };

    let tokens = vec![
        Token::Address(H160::from_slice(&address.to_bytes().unwrap())),
        Token::Uint(amount),
    ];

    func.encode_input(&tokens).unwrap()
}

/// Represents the parameters for an Ethereum transaction
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EthereumTransactionParameters {
    /// The address of the receiver
    pub receiver: EthereumAddress,
    /// The amount (in wei)
    pub amount: EthereumAmount,
    /// The transaction gas limit
    pub gas: U256,
    /// The transaction gas price in wei
    pub gas_price: EthereumAmount,
    /// The nonce of the Ethereum account
    pub nonce: U256,
    /// The transaction data
    pub data: Vec<u8>,
}

/// Represents an Ethereum transaction signature
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
struct EthereumTransactionSignature {
    /// The V field of the signature protected with a chain_id
    v: Vec<u8>,
    /// The R field of the signature
    r: Vec<u8>,
    /// The S field of the signature
    s: Vec<u8>,
}

/// Represents an Ethereum transaction id
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct EthereumTransactionId {
    pub txid: Vec<u8>,
}

impl TransactionId for EthereumTransactionId {}

impl fmt::Display for EthereumTransactionId {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "0x{}", hex::encode(&self.txid))
    }
}

/// Represents an Ethereum transaction
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct EthereumTransaction<N: EthereumNetwork> {
    /// The address of the sender
    sender: Option<EthereumAddress>,
    /// The transaction parameters (gas, gas_price, nonce, data)
    parameters: EthereumTransactionParameters,
    /// The transaction signature
    signature: Option<EthereumTransactionSignature>,
    _network: PhantomData<N>,
}

impl<N: EthereumNetwork> Transaction for EthereumTransaction<N> {
    type Address = EthereumAddress;
    type Format = EthereumFormat;
    type PublicKey = EthereumPublicKey;
    type TransactionId = EthereumTransactionId;
    type TransactionParameters = EthereumTransactionParameters;

    /// Returns an unsigned transaction given the transaction parameters.
    fn new(parameters: &Self::TransactionParameters) -> Result<Self, TransactionError> {
        Ok(Self {
            sender: None,
            parameters: parameters.clone(),
            signature: None,
            _network: PhantomData,
        })
    }

    /// Returns a signed transaction given the {r,s,recid}.
    fn sign(&mut self, rs: Vec<u8>, recid: u8) -> Result<Vec<u8>, TransactionError> {
        let message = libsecp256k1::Message::parse_slice(&self.to_transaction_id()?.txid)?;
        let recovery_id = libsecp256k1::RecoveryId::parse(recid)?;

        let public_key = EthereumPublicKey::from_secp256k1_public_key(libsecp256k1::recover(
            &message,
            &libsecp256k1::Signature::parse_standard_slice(rs.as_slice())?,
            &recovery_id,
        )?);
        self.sender = Some(public_key.to_address(&EthereumFormat::Standard)?);
        self.signature = Some(EthereumTransactionSignature {
            v: (u32::from(recid) + N::CHAIN_ID * 2 + 35)
                .to_be_bytes()
                .to_vec(), // EIP155
            r: rs[..32].to_vec(),
            s: rs[32..64].to_vec(),
        });
        self.to_bytes()
    }

    /// Returns a transaction given the transaction bytes.
    /// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
    fn from_bytes(transaction: &[u8]) -> Result<Self, TransactionError> {
        let list: Vec<Vec<u8>> = decode_list(transaction);
        if list.len() != 9 {
            return Err(TransactionError::InvalidRlpLength(list.len()));
        }

        let parameters = EthereumTransactionParameters {
            receiver: EthereumAddress::from_str(&hex::encode(&list[3]))?,
            amount: match list[4].is_empty() {
                true => EthereumAmount::from_u256(U256::zero()),
                false => EthereumAmount::from_u256(U256::from(list[4].as_slice())),
            },
            gas: match list[2].is_empty() {
                true => U256::zero(),
                false => U256::from(list[2].as_slice()),
            },
            gas_price: match list[1].is_empty() {
                true => EthereumAmount::from_u256(U256::zero()),
                false => EthereumAmount::from_u256(U256::from(list[1].as_slice())),
            },
            nonce: match list[0].is_empty() {
                true => U256::zero(),
                false => U256::from(list[0].as_slice()),
            },
            data: list[5].clone(),
        };

        match list[7].is_empty() && list[8].is_empty() {
            true => {
                // Raw transaction
                Ok(Self {
                    sender: None,
                    parameters,
                    signature: None,
                    _network: PhantomData,
                })
            }
            false => {
                // Signed transaction
                let mut v = list[6].clone();
                pad_zeros(&mut v, 4);
                let v: [u8; 4] = v.try_into().unwrap();
                let v = u32::from_be_bytes(v);
                let recovery_id =
                    libsecp256k1::RecoveryId::parse((v - N::CHAIN_ID * 2 - 35) as u8)?;
                let mut r = list[7].clone();
                pad_zeros(&mut r, 32);
                let mut s = list[8].clone();
                pad_zeros(&mut s, 32);
                let signature = [r.clone(), s.clone()].concat();
                let raw_transaction = Self {
                    sender: None,
                    parameters: parameters.clone(),
                    signature: None,
                    _network: PhantomData,
                };
                let message =
                    libsecp256k1::Message::parse_slice(&raw_transaction.to_transaction_id()?.txid)?;
                let public_key =
                    EthereumPublicKey::from_secp256k1_public_key(libsecp256k1::recover(
                        &message,
                        &libsecp256k1::Signature::parse_standard_slice(signature.as_slice())?,
                        &recovery_id,
                    )?);

                Ok(Self {
                    sender: Some(public_key.to_address(&EthereumFormat::Standard)?),
                    parameters,
                    signature: Some(EthereumTransactionSignature {
                        v: v.to_be_bytes().to_vec(),
                        r,
                        s,
                    }),
                    _network: PhantomData,
                })
            }
        }
    }

    /// Returns the transaction in bytes.
    /// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md
    fn to_bytes(&self) -> Result<Vec<u8>, TransactionError> {
        // Returns an encoded transaction in Recursive Length Prefix (RLP) format.
        // https://github.com/ethereum/wiki/wiki/RLP
        fn encode_transaction(
            transaction_rlp: &mut RlpStream,
            parameters: &EthereumTransactionParameters,
        ) -> Result<(), TransactionError> {
            transaction_rlp.append(&parameters.nonce);
            transaction_rlp.append(&parameters.gas_price.0);
            transaction_rlp.append(&parameters.gas);
            transaction_rlp.append(&hex::decode(&parameters.receiver.to_string()[2..])?);
            transaction_rlp.append(&parameters.amount.0);
            transaction_rlp.append(&parameters.data);
            Ok(())
        }

        // Returns the raw transaction (in RLP).
        fn raw_transaction<N: EthereumNetwork>(
            parameters: &EthereumTransactionParameters,
        ) -> Result<RlpStream, TransactionError> {
            let mut transaction_rlp = RlpStream::new();
            transaction_rlp.begin_list(9);
            encode_transaction(&mut transaction_rlp, parameters)?;
            let chain_id = N::CHAIN_ID.to_be_bytes().to_vec();
            let chain_id = trim_leading_zeros(&chain_id);
            transaction_rlp.append(&chain_id);
            transaction_rlp.append(&0u8);
            transaction_rlp.append(&0u8);
            Ok(transaction_rlp)
        }

        // Returns the signed transaction (in RLP).
        fn signed_transaction(
            parameters: &EthereumTransactionParameters,
            signature: &EthereumTransactionSignature,
        ) -> Result<RlpStream, TransactionError> {
            let mut transaction_rlp = RlpStream::new();
            transaction_rlp.begin_list(9);
            encode_transaction(&mut transaction_rlp, parameters)?;
            // trim the leading zeros of v
            let v = trim_leading_zeros(&signature.v);
            transaction_rlp.append(&v);
            // trim the leading zeros of r
            let r = trim_leading_zeros(&signature.r);
            transaction_rlp.append(&r);
            // trim the leading zeros of s
            let s = trim_leading_zeros(&signature.s);
            transaction_rlp.append(&s);
            Ok(transaction_rlp)
        }

        match &self.signature {
            Some(signature) => Ok(signed_transaction(&self.parameters, signature)?
                .out()
                .to_vec()),
            None => Ok(raw_transaction::<N>(&self.parameters)?.out().to_vec()),
        }
    }

    /// Returns the hash of the signed transaction, if the signature is present.
    /// Otherwise, returns the hash of the raw transaction.
    fn to_transaction_id(&self) -> Result<Self::TransactionId, TransactionError> {
        Ok(Self::TransactionId {
            txid: Vec::<u8>::from(&keccak256(&self.to_bytes()?)[..]),
        })
    }
}

impl<N: EthereumNetwork> EthereumTransaction<N> {
    pub fn get_from(&self) -> EthereumAddress {
        self.sender.clone().unwrap()
    }

    pub fn get_to(&self) -> EthereumAddress {
        self.parameters.receiver.clone()
    }

    pub fn get_amount(&self) -> EthereumAmount {
        self.parameters.amount
    }

    pub fn get_gas_price(&self) -> EthereumAmount {
        self.parameters.gas_price
    }

    pub fn get_gas_limit(&self) -> U256 {
        self.parameters.gas
    }

    pub fn get_nonce(&self) -> U256 {
        self.parameters.nonce
    }

    pub fn get_data(&self) -> Vec<u8> {
        self.parameters.data.clone()
    }

    pub fn get_r(&self) -> String {
        hex::encode(self.signature.clone().unwrap().r)
    }

    pub fn get_s(&self) -> String {
        hex::encode(self.signature.clone().unwrap().s)
    }

    pub fn get_v(&self) -> u32 {
        let v = self.signature.clone().unwrap().v;
        let v: [u8; 4] = v.try_into().unwrap();
        u32::from_be_bytes(v)
    }

    pub fn get_chain_id(&self) -> u32 {
        N::CHAIN_ID
    }
}

impl<N: EthereumNetwork> FromStr for EthereumTransaction<N> {
    type Err = TransactionError;

    fn from_str(tx: &str) -> Result<Self, Self::Err> {
        let tx = match &tx[..2] {
            "0x" => &tx[2..],
            _ => tx,
        };
        Self::from_bytes(&hex::decode(tx)?)
    }
}

impl<N: EthereumNetwork> fmt::Display for EthereumTransaction<N> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "0x{}",
            &hex::encode(match self.to_bytes() {
                Ok(transaction) => transaction,
                _ => return Err(fmt::Error),
            })
        )
    }
}