alloy_dyn_abi/eip712/
resolver.rs

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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use crate::{
    eip712::typed_data::Eip712Types, eip712_parser::EncodeType, DynSolType, DynSolValue, Error,
    Result, Specifier,
};
use alloc::{
    borrow::ToOwned,
    collections::{BTreeMap, BTreeSet},
    string::{String, ToString},
    vec::Vec,
};
use alloy_primitives::{keccak256, B256};
use alloy_sol_types::SolStruct;
use core::{cmp::Ordering, fmt};
use parser::{RootType, TypeSpecifier, TypeStem};
use serde::{Deserialize, Deserializer, Serialize};

/// An EIP-712 property definition.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize)]
pub struct PropertyDef {
    /// Typename.
    #[serde(rename = "type")]
    type_name: String,
    /// Property Name.
    name: String,
}

impl<'de> Deserialize<'de> for PropertyDef {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        struct PropertyDefHelper {
            #[serde(rename = "type")]
            type_name: String,
            name: String,
        }
        let h = PropertyDefHelper::deserialize(deserializer)?;
        Self::new(h.type_name, h.name).map_err(serde::de::Error::custom)
    }
}

impl PropertyDef {
    /// Instantiate a new name-type pair.
    #[inline]
    pub fn new<T, N>(type_name: T, name: N) -> Result<Self>
    where
        T: Into<String>,
        N: Into<String>,
    {
        let type_name = type_name.into();
        TypeSpecifier::parse(type_name.as_str())?;
        Ok(Self::new_unchecked(type_name, name))
    }

    /// Instantiate a new name-type pair, without checking that the type name
    /// is a valid root type.
    #[inline]
    pub fn new_unchecked<T, N>(type_name: T, name: N) -> Self
    where
        T: Into<String>,
        N: Into<String>,
    {
        Self { type_name: type_name.into(), name: name.into() }
    }

    /// Returns the name of the property.
    #[inline]
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Returns the type name of the property.
    #[inline]
    pub fn type_name(&self) -> &str {
        &self.type_name
    }

    /// Returns the root type of the name/type pair, stripping any array.
    #[inline]
    pub fn root_type_name(&self) -> &str {
        self.type_name.split_once('[').map(|t| t.0).unwrap_or(&self.type_name)
    }
}

/// An EIP-712 type definition.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct TypeDef {
    /// Must always be a ROOT type name with any array stripped.
    type_name: String,
    /// A list of property definitions.
    props: Vec<PropertyDef>,
}

impl Ord for TypeDef {
    // This is not a logic error because we know type names cannot be duplicated in
    // the resolver map
    fn cmp(&self, other: &Self) -> Ordering {
        self.type_name.cmp(&other.type_name)
    }
}

impl PartialOrd for TypeDef {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl fmt::Display for TypeDef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.fmt_eip712_encode_type(f)
    }
}

impl TypeDef {
    /// Instantiate a new type definition, checking that the type name is a
    /// valid root type.
    #[inline]
    pub fn new<S: Into<String>>(type_name: S, props: Vec<PropertyDef>) -> Result<Self> {
        let type_name = type_name.into();
        RootType::parse(type_name.as_str())?;
        Ok(Self { type_name, props })
    }

    /// Instantiate a new type definition, without checking that the type name
    /// is a valid root type. This may result in bad behavior in a resolver.
    #[inline]
    pub const fn new_unchecked(type_name: String, props: Vec<PropertyDef>) -> Self {
        Self { type_name, props }
    }

    /// Returns the type name of the type definition.
    #[inline]
    pub fn type_name(&self) -> &str {
        &self.type_name
    }

    /// Returns the property definitions of the type definition.
    #[inline]
    pub fn props(&self) -> &[PropertyDef] {
        &self.props
    }

    /// Returns the property names of the type definition.
    #[inline]
    pub fn prop_names(&self) -> impl Iterator<Item = &str> + '_ {
        self.props.iter().map(|p| p.name())
    }

    /// Returns the root property types of the type definition.
    #[inline]
    pub fn prop_root_types(&self) -> impl Iterator<Item = &str> + '_ {
        self.props.iter().map(|p| p.root_type_name())
    }

    /// Returns the property types of the type definition.
    #[inline]
    pub fn prop_types(&self) -> impl Iterator<Item = &str> + '_ {
        self.props.iter().map(|p| p.type_name())
    }

    /// Produces the EIP-712 `encodeType` typestring for this type definition.
    #[inline]
    pub fn eip712_encode_type(&self) -> String {
        let mut s = String::with_capacity(self.type_name.len() + 2 + self.props_bytes_len());
        self.fmt_eip712_encode_type(&mut s).unwrap();
        s
    }

    /// Formats the EIP-712 `encodeType` typestring for this type definition
    /// into `f`.
    pub fn fmt_eip712_encode_type(&self, f: &mut impl fmt::Write) -> fmt::Result {
        f.write_str(&self.type_name)?;
        f.write_char('(')?;
        for (i, prop) in self.props.iter().enumerate() {
            if i > 0 {
                f.write_char(',')?;
            }

            f.write_str(prop.type_name())?;
            f.write_char(' ')?;
            f.write_str(prop.name())?;
        }
        f.write_char(')')
    }

    /// Returns the number of bytes that the properties of this type definition
    /// will take up when formatted in the EIP-712 `encodeType` typestring.
    #[inline]
    pub fn props_bytes_len(&self) -> usize {
        self.props.iter().map(|p| p.type_name.len() + p.name.len() + 2).sum()
    }

    /// Return the root type.
    #[inline]
    pub fn root_type(&self) -> RootType<'_> {
        self.type_name.as_str().try_into().expect("checked in instantiation")
    }
}

#[derive(Debug, Default)]
struct DfsContext<'a> {
    visited: BTreeSet<&'a TypeDef>,
    stack: BTreeSet<&'a str>,
}

/// A dependency graph built from the `Eip712Types` object. This is used to
/// safely resolve JSON into a [`crate::DynSolType`] by detecting cycles in the
/// type graph and traversing the dep graph.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Resolver {
    /// Nodes in the graph
    // NOTE: Non-duplication of names must be enforced. See note on impl of Ord
    // for TypeDef
    nodes: BTreeMap<String, TypeDef>,
    /// Edges from a type name to its dependencies.
    edges: BTreeMap<String, Vec<String>>,
}

impl Serialize for Resolver {
    #[inline]
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        Eip712Types::from(self).serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for Resolver {
    #[inline]
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        Eip712Types::deserialize(deserializer).map(Into::into)
    }
}

impl From<Eip712Types> for Resolver {
    fn from(types: Eip712Types) -> Self {
        Self::from(&types)
    }
}

impl From<&Eip712Types> for Resolver {
    #[inline]
    fn from(types: &Eip712Types) -> Self {
        let mut graph = Self::default();
        graph.ingest_types(types);
        graph
    }
}

impl From<&Resolver> for Eip712Types {
    fn from(resolver: &Resolver) -> Self {
        let mut types = Self::default();
        for (name, ty) in &resolver.nodes {
            types.insert(name.clone(), ty.props.clone());
        }
        types
    }
}

impl Resolver {
    /// Instantiate a new resolver from a `SolStruct` type.
    pub fn from_struct<S: SolStruct>() -> Self {
        let mut resolver = Self::default();
        resolver.ingest_sol_struct::<S>();
        resolver
    }

    /// Detect cycles in the subgraph rooted at `type_name`
    fn detect_cycle<'a>(&'a self, type_name: &str, context: &mut DfsContext<'a>) -> bool {
        let ty = match self.nodes.get(type_name) {
            Some(ty) => ty,
            None => return false,
        };

        if context.stack.contains(type_name) {
            return true;
        }
        if context.visited.contains(ty) {
            return false;
        }

        // update visited and stack
        context.visited.insert(ty);
        context.stack.insert(&ty.type_name);

        if self
            .edges
            .get(&ty.type_name)
            .unwrap()
            .iter()
            .any(|edge| self.detect_cycle(edge, context))
        {
            return true;
        }

        context.stack.remove(type_name);
        false
    }

    /// Ingest types from an EIP-712 `encodeType`.
    pub fn ingest_string(&mut self, s: impl AsRef<str>) -> Result<()> {
        let encode_type: EncodeType<'_> = s.as_ref().try_into()?;
        for t in encode_type.types {
            self.ingest(t.to_owned());
        }
        Ok(())
    }

    /// Ingest a sol struct typedef.
    pub fn ingest_sol_struct<S: SolStruct>(&mut self) {
        self.ingest_string(S::eip712_encode_type()).unwrap();
    }

    /// Ingest a type.
    pub fn ingest(&mut self, type_def: TypeDef) {
        let type_name = type_def.type_name.to_owned();
        // Insert the edges into the graph
        {
            let entry = self.edges.entry(type_name.clone()).or_default();
            for prop in &type_def.props {
                entry.push(prop.root_type_name().to_owned());
            }
        } // entry dropped here

        // Insert the node into the graph
        self.nodes.insert(type_name, type_def);
    }

    /// Ingest a `Types` object into the resolver, discarding any invalid types.
    pub fn ingest_types(&mut self, types: &Eip712Types) {
        for (type_name, props) in types {
            if let Ok(ty) = TypeDef::new(type_name.clone(), props.to_vec()) {
                self.ingest(ty);
            }
        }
    }

    // This function assumes that the graph is acyclic.
    fn linearize_into<'a>(
        &'a self,
        resolution: &mut Vec<&'a TypeDef>,
        root_type: RootType<'_>,
    ) -> Result<()> {
        if root_type.try_basic_solidity().is_ok() {
            return Ok(());
        }

        let this_type = self
            .nodes
            .get(root_type.span())
            .ok_or_else(|| Error::missing_type(root_type.span()))?;

        let edges: &Vec<String> = self.edges.get(root_type.span()).unwrap();

        if !resolution.contains(&this_type) {
            resolution.push(this_type);
            for edge in edges {
                let rt = edge.as_str().try_into()?;
                self.linearize_into(resolution, rt)?;
            }
        }

        Ok(())
    }

    /// This function linearizes a type into a list of typedefs of its
    /// dependencies.
    pub fn linearize(&self, type_name: &str) -> Result<Vec<&TypeDef>> {
        let mut context = DfsContext::default();
        if self.detect_cycle(type_name, &mut context) {
            return Err(Error::circular_dependency(type_name));
        }
        let root_type = type_name.try_into()?;
        let mut resolution = vec![];
        self.linearize_into(&mut resolution, root_type)?;
        Ok(resolution)
    }

    /// Resolve a typename into a [`crate::DynSolType`] or return an error if
    /// the type is missing, or contains a circular dependency.
    pub fn resolve(&self, type_name: &str) -> Result<DynSolType> {
        if self.detect_cycle(type_name, &mut Default::default()) {
            return Err(Error::circular_dependency(type_name));
        }
        self.unchecked_resolve(&type_name.try_into()?)
    }

    /// Resolve a type into a [`crate::DynSolType`] without checking for cycles.
    fn unchecked_resolve(&self, type_spec: &TypeSpecifier<'_>) -> Result<DynSolType> {
        let ty = match &type_spec.stem {
            TypeStem::Root(root) => self.resolve_root_type(*root),
            TypeStem::Tuple(tuple) => tuple
                .types
                .iter()
                .map(|ty| self.unchecked_resolve(ty))
                .collect::<Result<_, _>>()
                .map(DynSolType::Tuple),
        }?;
        Ok(ty.array_wrap_from_iter(type_spec.sizes.iter().copied()))
    }

    /// Resolves a root Solidity type into either a basic type or a custom
    /// struct.
    fn resolve_root_type(&self, root_type: RootType<'_>) -> Result<DynSolType> {
        if let Ok(ty) = root_type.resolve() {
            return Ok(ty);
        }

        let ty = self
            .nodes
            .get(root_type.span())
            .ok_or_else(|| Error::missing_type(root_type.span()))?;

        let prop_names: Vec<_> = ty.prop_names().map(str::to_string).collect();
        let tuple: Vec<_> = ty
            .prop_types()
            .map(|ty| self.unchecked_resolve(&ty.try_into()?))
            .collect::<Result<_, _>>()?;

        Ok(DynSolType::CustomStruct { name: ty.type_name.clone(), prop_names, tuple })
    }

    /// Encode the type into an EIP-712 `encodeType` string
    ///
    /// <https://eips.ethereum.org/EIPS/eip-712#definition-of-encodetype>
    pub fn encode_type(&self, name: &str) -> Result<String> {
        let linear = self.linearize(name)?;
        let first = linear.first().unwrap().eip712_encode_type();

        // Sort references by name (eip-712 encodeType spec)
        let mut sorted_refs =
            linear[1..].iter().map(|t| t.eip712_encode_type()).collect::<Vec<String>>();
        sorted_refs.sort();

        Ok(sorted_refs.iter().fold(first, |mut acc, s| {
            acc.push_str(s);
            acc
        }))
    }

    /// Compute the keccak256 hash of the EIP-712 `encodeType` string.
    pub fn type_hash(&self, name: &str) -> Result<B256> {
        self.encode_type(name).map(keccak256)
    }

    /// Encode the data according to EIP-712 `encodeData` rules.
    pub fn encode_data(&self, value: &DynSolValue) -> Result<Option<Vec<u8>>> {
        Ok(match value {
            DynSolValue::CustomStruct { tuple: inner, .. }
            | DynSolValue::Array(inner)
            | DynSolValue::FixedArray(inner) => {
                let mut bytes = Vec::with_capacity(inner.len() * 32);
                for v in inner {
                    bytes.extend(self.eip712_data_word(v)?.as_slice());
                }
                Some(bytes)
            }
            DynSolValue::Bytes(buf) => Some(buf.to_vec()),
            DynSolValue::String(s) => Some(s.as_bytes().to_vec()),
            _ => None,
        })
    }

    /// Encode the data as a struct property according to EIP-712 `encodeData`
    /// rules. Atomic types are encoded as-is, while non-atomic types are
    /// encoded as their `encodeData` hash.
    pub fn eip712_data_word(&self, value: &DynSolValue) -> Result<B256> {
        if let Some(word) = value.as_word() {
            return Ok(word);
        }

        let mut bytes;
        let to_hash = match value {
            DynSolValue::CustomStruct { name, tuple, .. } => {
                bytes = self.type_hash(name)?.to_vec();
                for v in tuple {
                    bytes.extend(self.eip712_data_word(v)?.as_slice());
                }
                &bytes[..]
            }
            DynSolValue::Array(inner) | DynSolValue::FixedArray(inner) => {
                bytes = Vec::with_capacity(inner.len() * 32);
                for v in inner {
                    bytes.extend(self.eip712_data_word(v)?);
                }
                &bytes[..]
            }
            DynSolValue::Bytes(buf) => buf,
            DynSolValue::String(s) => s.as_bytes(),
            _ => unreachable!("all types are words or covered in the match"),
        };
        Ok(keccak256(to_hash))
    }

    /// Check if the resolver graph contains a type by its name.
    ///
    /// ## Warning
    ///
    /// This checks by NAME only. It does NOT check for type
    pub fn contains_type_name(&self, name: &str) -> bool {
        self.nodes.contains_key(name)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::boxed::Box;
    use alloy_sol_types::sol;

    #[test]
    fn it_detects_cycles() {
        let mut graph = Resolver::default();
        graph.ingest(TypeDef::new_unchecked(
            "A".to_string(),
            vec![PropertyDef::new_unchecked("B", "myB")],
        ));
        graph.ingest(TypeDef::new_unchecked(
            "B".to_string(),
            vec![PropertyDef::new_unchecked("C", "myC")],
        ));
        graph.ingest(TypeDef::new_unchecked(
            "C".to_string(),
            vec![PropertyDef::new_unchecked("A", "myA")],
        ));

        assert!(graph.detect_cycle("A", &mut DfsContext::default()));
    }

    #[test]
    fn it_produces_encode_type_strings() {
        let mut graph = Resolver::default();
        graph.ingest(TypeDef::new_unchecked(
            "A".to_string(),
            vec![PropertyDef::new_unchecked("C", "myC"), PropertyDef::new_unchecked("B", "myB")],
        ));
        graph.ingest(TypeDef::new_unchecked(
            "B".to_string(),
            vec![PropertyDef::new_unchecked("C", "myC")],
        ));
        graph.ingest(TypeDef::new_unchecked(
            "C".to_string(),
            vec![
                PropertyDef::new_unchecked("uint256", "myUint"),
                PropertyDef::new_unchecked("uint256", "myUint2"),
            ],
        ));

        // This tests specific adherence to EIP-712 specified ordering.
        // Referenced types are sorted by name, the Primary type is at the
        // start of the string
        assert_eq!(
            graph.encode_type("A").unwrap(),
            "A(C myC,B myB)B(C myC)C(uint256 myUint,uint256 myUint2)"
        );
    }

    #[test]
    fn it_resolves_types() {
        let mut graph = Resolver::default();
        graph.ingest(TypeDef::new_unchecked(
            "A".to_string(),
            vec![PropertyDef::new_unchecked("B", "myB")],
        ));
        graph.ingest(TypeDef::new_unchecked(
            "B".to_string(),
            vec![PropertyDef::new_unchecked("C", "myC")],
        ));
        graph.ingest(TypeDef::new_unchecked(
            "C".to_string(),
            vec![PropertyDef::new_unchecked("uint256", "myUint")],
        ));

        let c = DynSolType::CustomStruct {
            name: "C".to_string(),
            prop_names: vec!["myUint".to_string()],
            tuple: vec![DynSolType::Uint(256)],
        };
        let b = DynSolType::CustomStruct {
            name: "B".to_string(),
            prop_names: vec!["myC".to_string()],
            tuple: vec![c.clone()],
        };
        let a = DynSolType::CustomStruct {
            name: "A".to_string(),
            prop_names: vec!["myB".to_string()],
            tuple: vec![b.clone()],
        };
        assert_eq!(graph.resolve("A"), Ok(a));
        assert_eq!(graph.resolve("B"), Ok(b));
        assert_eq!(graph.resolve("C"), Ok(c));
    }

    #[test]
    fn it_resolves_types_with_arrays() {
        let mut graph = Resolver::default();
        graph.ingest(TypeDef::new_unchecked(
            "A".to_string(),
            vec![PropertyDef::new_unchecked("B", "myB")],
        ));
        graph.ingest(TypeDef::new_unchecked(
            "B".to_string(),
            vec![PropertyDef::new_unchecked("C[]", "myC")],
        ));
        graph.ingest(TypeDef::new_unchecked(
            "C".to_string(),
            vec![PropertyDef::new_unchecked("uint256", "myUint")],
        ));

        let c = DynSolType::CustomStruct {
            name: "C".to_string(),
            prop_names: vec!["myUint".to_string()],
            tuple: vec![DynSolType::Uint(256)],
        };
        let b = DynSolType::CustomStruct {
            name: "B".to_string(),
            prop_names: vec!["myC".to_string()],
            tuple: vec![DynSolType::Array(Box::new(c.clone()))],
        };
        let a = DynSolType::CustomStruct {
            name: "A".to_string(),
            prop_names: vec!["myB".to_string()],
            tuple: vec![b.clone()],
        };
        assert_eq!(graph.resolve("C"), Ok(c));
        assert_eq!(graph.resolve("B"), Ok(b));
        assert_eq!(graph.resolve("A"), Ok(a));
    }

    #[test]
    fn encode_type_round_trip() {
        const ENCODE_TYPE: &str = "A(C myC,B myB)B(C myC)C(uint256 myUint,uint256 myUint2)";
        let mut graph = Resolver::default();
        graph.ingest_string(ENCODE_TYPE).unwrap();
        assert_eq!(graph.encode_type("A").unwrap(), ENCODE_TYPE);

        const ENCODE_TYPE_2: &str = "Transaction(Person from,Person to,Asset tx)Asset(address token,uint256 amount)Person(address wallet,string name)";
        let mut graph = Resolver::default();
        graph.ingest_string(ENCODE_TYPE_2).unwrap();
        assert_eq!(graph.encode_type("Transaction").unwrap(), ENCODE_TYPE_2);
    }

    #[test]
    fn it_ingests_sol_structs() {
        sol!(
            struct MyStruct {
                uint256 a;
            }
        );

        let mut graph = Resolver::default();
        graph.ingest_sol_struct::<MyStruct>();
        assert_eq!(graph.encode_type("MyStruct").unwrap(), MyStruct::eip712_encode_type());
    }
}