forge/cmd/
eip712.rs

1use alloy_primitives::{B256, keccak256};
2use clap::{Parser, ValueHint};
3use eyre::Result;
4use foundry_cli::{opts::BuildOpts, utils::LoadConfig};
5use foundry_common::{compile::ProjectCompiler, shell};
6use serde::Serialize;
7use solar::sema::{
8    Gcx, Hir,
9    hir::StructId,
10    ty::{Ty, TyKind},
11};
12use std::{
13    collections::BTreeMap,
14    fmt::{Display, Formatter, Result as FmtResult, Write},
15    ops::ControlFlow,
16    path::{Path, PathBuf},
17};
18
19foundry_config::impl_figment_convert!(Eip712Args, build);
20
21/// CLI arguments for `forge eip712`.
22#[derive(Clone, Debug, Parser)]
23pub struct Eip712Args {
24    /// The path to the file from which to read struct definitions.
25    #[arg(value_hint = ValueHint::FilePath, value_name = "PATH")]
26    pub target_path: PathBuf,
27
28    #[command(flatten)]
29    build: BuildOpts,
30}
31
32#[derive(Debug, Serialize)]
33struct Eip712Output {
34    path: String,
35    #[serde(rename = "type")]
36    ty: String,
37    hash: B256,
38}
39
40impl Display for Eip712Output {
41    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
42        writeln!(f, "{}:", self.path)?;
43        writeln!(f, " - type: {}", self.ty)?;
44        writeln!(f, " - hash: {}", self.hash)
45    }
46}
47
48impl Eip712Args {
49    pub fn run(self) -> Result<()> {
50        let config = self.build.load_config()?;
51        let project = config.solar_project()?;
52        let mut output = ProjectCompiler::new().files([self.target_path]).compile(&project)?;
53        let compiler = output.parser_mut().solc_mut().compiler_mut();
54        compiler.enter_mut(|compiler| -> Result<()> {
55            let Ok(ControlFlow::Continue(())) = compiler.lower_asts() else { return Ok(()) };
56            let gcx = compiler.gcx();
57            let resolver = Resolver::new(gcx);
58
59            let outputs = resolver
60                .struct_ids()
61                .filter_map(|id| {
62                    let resolved = resolver.resolve_struct_eip712(id)?;
63                    Some(Eip712Output {
64                        path: resolver.get_struct_path(id),
65                        hash: keccak256(resolved.as_bytes()),
66                        ty: resolved,
67                    })
68                })
69                .collect::<Vec<_>>();
70
71            if shell::is_json() {
72                sh_println!("{json}", json = serde_json::to_string_pretty(&outputs)?)?;
73            } else {
74                for output in &outputs {
75                    sh_println!("{output}")?;
76                }
77            }
78
79            Ok(())
80        })?;
81
82        // `compiler.sess()` inside of `ProjectCompileOutput` is built with `with_buffer_emitter`.
83        let diags = compiler.sess().dcx.emitted_diagnostics().unwrap();
84        if compiler.sess().dcx.has_errors().is_err() {
85            eyre::bail!("{diags}");
86        } else {
87            let _ = sh_print!("{diags}");
88        }
89
90        Ok(())
91    }
92}
93
94/// Generates the EIP-712 `encodeType` string for a given struct.
95///
96/// Requires a reference to the source HIR.
97pub struct Resolver<'gcx> {
98    gcx: Gcx<'gcx>,
99}
100
101impl<'gcx> Resolver<'gcx> {
102    /// Constructs a new [`Resolver`] for the supplied [`Hir`] instance.
103    pub fn new(gcx: Gcx<'gcx>) -> Self {
104        Self { gcx }
105    }
106
107    #[inline]
108    fn hir(&self) -> &'gcx Hir<'gcx> {
109        &self.gcx.hir
110    }
111
112    /// Returns the [`StructId`]s of every user-defined struct in source order.
113    pub fn struct_ids(&self) -> impl Iterator<Item = StructId> {
114        self.hir().strukt_ids()
115    }
116
117    /// Returns the path for a struct, with the format: `file.sol > MyContract > MyStruct`
118    pub fn get_struct_path(&self, id: StructId) -> String {
119        let strukt = self.hir().strukt(id).name.as_str();
120        match self.hir().strukt(id).contract {
121            Some(cid) => {
122                let full_name = self.gcx.contract_fully_qualified_name(cid).to_string();
123                let relevant = Path::new(&full_name)
124                    .file_name()
125                    .and_then(|s| s.to_str())
126                    .unwrap_or(&full_name);
127
128                if let Some((file, contract)) = relevant.rsplit_once(':') {
129                    format!("{file} > {contract} > {strukt}")
130                } else {
131                    format!("{relevant} > {strukt}")
132                }
133            }
134            None => strukt.to_string(),
135        }
136    }
137
138    /// Converts a given struct into its EIP-712 `encodeType` representation.
139    ///
140    /// Returns `None` if the struct, or any of its fields, contains constructs
141    /// not supported by EIP-712 (mappings, function types, errors, etc).
142    pub fn resolve_struct_eip712(&self, id: StructId) -> Option<String> {
143        let mut subtypes = BTreeMap::new();
144        subtypes.insert(self.hir().strukt(id).name.as_str().into(), id);
145        self.resolve_eip712_inner(id, &mut subtypes, true, None)
146    }
147
148    fn resolve_eip712_inner(
149        &self,
150        id: StructId,
151        subtypes: &mut BTreeMap<String, StructId>,
152        append_subtypes: bool,
153        rename: Option<&str>,
154    ) -> Option<String> {
155        let def = self.hir().strukt(id);
156        let mut result = format!("{}(", rename.unwrap_or(def.name.as_str()));
157
158        for (idx, field_id) in def.fields.iter().enumerate() {
159            let field = self.hir().variable(*field_id);
160            let ty = self.resolve_type(self.gcx.type_of_hir_ty(&field.ty), subtypes)?;
161
162            write!(result, "{ty} {name}", name = field.name?.as_str()).ok()?;
163
164            if idx < def.fields.len() - 1 {
165                result.push(',');
166            }
167        }
168
169        result.push(')');
170
171        if append_subtypes {
172            for (subtype_name, subtype_id) in
173                subtypes.iter().map(|(name, id)| (name.clone(), *id)).collect::<Vec<_>>()
174            {
175                if subtype_id == id {
176                    continue;
177                }
178                let encoded_subtype =
179                    self.resolve_eip712_inner(subtype_id, subtypes, false, Some(&subtype_name))?;
180
181                result.push_str(&encoded_subtype);
182            }
183        }
184
185        Some(result)
186    }
187
188    fn resolve_type(
189        &self,
190        ty: Ty<'gcx>,
191        subtypes: &mut BTreeMap<String, StructId>,
192    ) -> Option<String> {
193        let ty = ty.peel_refs();
194        match ty.kind {
195            TyKind::Elementary(elem_ty) => Some(elem_ty.to_abi_str().to_string()),
196            TyKind::Array(element_ty, size) => {
197                let inner_type = self.resolve_type(element_ty, subtypes)?;
198                let size = size.to_string();
199                Some(format!("{inner_type}[{size}]"))
200            }
201            TyKind::DynArray(element_ty) => {
202                let inner_type = self.resolve_type(element_ty, subtypes)?;
203                Some(format!("{inner_type}[]"))
204            }
205            TyKind::Udvt(ty, _) => self.resolve_type(ty, subtypes),
206            TyKind::Struct(id) => {
207                let def = self.hir().strukt(id);
208                let name = match subtypes.iter().find(|(_, cached_id)| id == **cached_id) {
209                    Some((name, _)) => name.to_string(),
210                    None => {
211                        // Otherwise, assign new name
212                        let mut i = 0;
213                        let mut name = def.name.as_str().into();
214                        while subtypes.contains_key(&name) {
215                            i += 1;
216                            name = format!("{}_{i}", def.name.as_str());
217                        }
218
219                        subtypes.insert(name.clone(), id);
220
221                        // Recursively resolve fields to populate subtypes
222                        for &field_id in def.fields {
223                            let field_ty = self.gcx.type_of_item(field_id.into());
224                            self.resolve_type(field_ty, subtypes)?;
225                        }
226                        name
227                    }
228                };
229
230                Some(name)
231            }
232            // For now, map enums to `uint8`
233            TyKind::Enum(_) => Some("uint8".to_string()),
234            // For now, map contracts to `address`
235            TyKind::Contract(_) => Some("address".to_string()),
236            // EIP-712 doesn't support tuples (should use structs), functions, mappings, nor errors
237            _ => None,
238        }
239    }
240}