ethers_eip2718/
lib.rs

1use ethers_primitives::*;
2use serde::{Deserialize, Serialize};
3
4macro_rules! tx_json_support {
5    ($ty:ident) => {
6        impl TryFrom<&str> for $ty {
7            type Error = serde_json::Error;
8
9            fn try_from(value: &str) -> Result<Self, Self::Error> {
10                serde_json::from_str(value)
11            }
12        }
13
14        impl TryFrom<String> for $ty {
15            type Error = serde_json::Error;
16            fn try_from(value: String) -> Result<Self, Self::Error> {
17                Self::try_from(value.as_ref())
18            }
19        }
20
21        impl TryFrom<serde_json::Value> for $ty {
22            type Error = serde_json::Error;
23            fn try_from(value: serde_json::Value) -> Result<Self, Self::Error> {
24                serde_json::from_value(value)
25            }
26        }
27    };
28}
29
30pub fn keccak256<S>(bytes: S) -> [u8; 32]
31where
32    S: AsRef<[u8]>,
33{
34    let mut hasher = Keccak256::new();
35
36    hasher.update(bytes.as_ref());
37
38    hasher.finalize().into()
39}
40
41#[derive(Debug, Serialize, Deserialize)]
42#[serde(untagged)]
43pub enum NameOrAddress {
44    Name(String),
45    Address(crate::Address),
46}
47
48use sha3::{Digest, Keccak256};
49
50tx_json_support!(LegacyTransactionRequest);
51tx_json_support!(Eip2930TransactionRequest);
52tx_json_support!(Eip1559TransactionRequest);
53
54#[derive(Serialize, Deserialize, Debug, Clone)]
55#[serde(tag = "type")]
56pub enum TypedTransactionRequest {
57    // 0x00
58    #[serde(rename = "0x00")]
59    Legacy(LegacyTransactionRequest),
60    // 0x01
61    #[serde(rename = "0x01")]
62    Eip2930(Eip2930TransactionRequest),
63    // 0x02
64    #[serde(rename = "0x02")]
65    Eip1559(Eip1559TransactionRequest),
66}
67
68impl From<LegacyTransactionRequest> for TypedTransactionRequest {
69    fn from(tx: LegacyTransactionRequest) -> Self {
70        TypedTransactionRequest::Legacy(tx)
71    }
72}
73
74impl From<Eip2930TransactionRequest> for TypedTransactionRequest {
75    fn from(tx: Eip2930TransactionRequest) -> Self {
76        TypedTransactionRequest::Eip2930(tx)
77    }
78}
79
80impl From<Eip1559TransactionRequest> for TypedTransactionRequest {
81    fn from(tx: Eip1559TransactionRequest) -> Self {
82        TypedTransactionRequest::Eip1559(tx)
83    }
84}
85
86impl TypedTransactionRequest {
87    pub fn sign_hash(&self) -> anyhow::Result<H256> {
88        match self {
89            Self::Legacy(tx) => tx.sign_hash(),
90            Self::Eip2930(tx) => tx.sign_hash(),
91            Self::Eip1559(tx) => tx.sign_hash(),
92        }
93    }
94
95    pub fn rlp_signed(&self, signature: Eip1559Signature) -> anyhow::Result<Bytes> {
96        match self {
97            Self::Legacy(tx) => tx.rlp_signed(signature),
98            Self::Eip2930(tx) => tx.rlp_signed(signature),
99            Self::Eip1559(tx) => tx.rlp_signed(signature),
100        }
101    }
102}
103
104mod accesslist;
105pub use accesslist::*;
106
107mod legacy;
108pub use legacy::*;
109
110mod eip2930;
111pub use eip2930::*;
112
113mod eip1559;
114pub use eip1559::*;